Set .has vs .includes with smallish string array (v2)

Revision 2 of this benchmark created on


Setup

const reservedWords = [
  'do',
  'if',
  'in',
  'for',
  'let',
  'new',
  'try',
  'var',
  'case',
  'else',
  'enum',
  'eval',
  'false',
  'null',
  'this',
  'true',
  'void',
  'with',
  'break',
  'catch',
  'class',
  'const',
  'super',
  'throw',
  'while',
  'yield',
  'delete',
  'export',
  'import',
  'public',
  'return',
  'static',
  'switch',
  'typeof',
  'default',
  'extends',
  'finally',
  'package',
  'private',
  'continue',
  'debugger',
  'function',
  'arguments',
  'interface',
  'protected',
  'implements',
  'instanceof',
  'abstract',
  'boolean',
  'byte',
  'char',
  'double',
  'final',
  'float',
  'goto',
  'int',
  'long',
  'native',
  'short',
  'synchronized',
  'throws',
  'transient',
  'volatile',
  // special
  'as',
  'async',
  'from',
  'get',
  'of',
  'set',
];

function randomReservedWord() {
	return reservedWords[Math.floor(Math.random() * reservedWords.length)];
}

const reservedWordsSet = new Set(reservedWords);

Test runner

Ready to run.

Testing in
TestOps/sec
.includes
let count = 0;

for (let i = 0; i < 1_000_000; i++) {
	const word = randomReservedWord();
	if (reservedWords.includes(word)) {
		count++;
	}
}

console.log(count);
ready
.has
let count = 0;

for (let i = 0; i < 1_000_000; i++) {
	const word = randomReservedWord();
	if (reservedWordsSet.has(word)) {
		count++;
	}
}

console.log(count);
ready

Revisions

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