Array .includes vs .find vs object lookup (1000 items) (v3)

Revision 3 of this benchmark created on


Description

Array .includes vs .find vs object lookup. 1000 items and value of 50 characters length.

Setup

var AMOUNT_OF_VALUES = 1000;
var STRING_VALUE_LENGTH = 50;

function generateRandomString() {
  return 'x'.repeat(STRING_VALUE_LENGTH).replace(/[x]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

var valuesArray = [...Array(AMOUNT_OF_VALUES)].map(_ => generateRandomString());

var valuesObject = valuesArray.reduce((acc, e) => ({ ...acc, [e]: true }), {});

var getRandomValue = () => valuesArray[Math.floor(Math.random() * valuesArray.length)];

Test runner

Ready to run.

Testing in
TestOps/sec
.includes
var randomValue = getRandomValue();
var result = valuesArray.includes(randomValue);
ready
.find
var randomValue = getRandomValue();
var result = !!valuesArray.find(value => value === randomValue);
ready
object lookup
var randomValue = getRandomValue();
var result = !!valuesObject[randomValue];
ready

Revisions

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