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

Benchmark created on


Setup

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

const validKeysSet = new Set(validKeysArray);


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

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
Using existing array best case
validKeysArray.includes(keyUp)
ready
Using existing array worst case
validKeysArray.includes(keyRight)
ready

Revisions

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