JS Array indxOf vs includes

Benchmark created on


Description

Let's see, why modern code linting favors more to includes which claimed to be more "stable" they said.. hahahaha

Setup

// Shared test data
var supportedLocalesSmall = ['en', 'en-US', 'de', 'fr', 'it', 'es'];
var supportedLocalesLarge = [];

for (var i = 0; i < 10000; i++) {
  supportedLocalesLarge.push('loc-' + i);
}

var hitLocaleSmall = 'fr';
var missLocaleSmall = 'jp';

var hitLocaleLarge = 'loc-9999';
var missLocaleLarge = 'loc-10001';

// Toggle which scenario you want to benchmark
// Uncomment ONE block at a time to keep results clean

// Small array - HIT
var supportedLocales = supportedLocalesSmall;
var locale = hitLocaleSmall;

// Small array - MISS
// var supportedLocales = supportedLocalesSmall;
// var locale = missLocaleSmall;

// Large array - HIT
// var supportedLocales = supportedLocalesLarge;
// var locale = hitLocaleLarge;

// Large array - MISS
// var supportedLocales = supportedLocalesLarge;
// var locale = missLocaleLarge;

Test runner

Ready to run.

Testing in
TestOps/sec
Array indexOf
if (supportedLocales.indexOf(locale) === -1) {
  return false;
}
return true;
ready
Array includes
if (!supportedLocales.includes(locale)) {
  return false;
}
return true;
ready

Revisions

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