Checking to see if a key is valid using an array or set (v2)

Revision 2 of this benchmark created on


Setup

const validKeysArray = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];

const validKeysSet = new Set(validKeysArray);

const keyRight = 'ArrowRight';
const keyUp = 'ArrowUp';

const isValidKey = key => (key === 'ArrowUp' || key === 'ArrowDown' || key === 'ArrowLeft' || key === 'ArrowRight');

Test runner

Ready to run.

Testing in
TestOps/sec
Reading from the set 1
validKeysSet.has(keyUp)
ready
Reading from the set 2
validKeysSet.has(keyRight)
ready
Creating array and checking best case
['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(keyUp);
ready
Creating array and checking worst case
['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(keyRight);
ready
If check best case
isValidKey(keyUp)
ready
If check worst case
isValidKey(keyRight)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.