[JavaScript] Array.includes vs Array.indexOf Performance Comparison

Tadashi Shigeoka ·  Thu, December 26, 2019

I checked the execution times of JavaScript Array.includes and Array.indexOf using jsPerf.

JavaScript

Background: Array.includes vs Array.indexOf - Which is Faster?

I’ll introduce the results of investigating which is faster between JavaScript Array.includes and Array.indexOf.

Result: Array.includes is Fastest

Test Environment: Chrome 79.0.3945 / Mac OS X 10.14.6

Array.includes vs Array.indexof

Array.includes is Also Faster in Node.js 8.x >=

According to the GitHub issue below, Array.includes is faster than Array.indexOf in Node.js 8.x.

I've tested it on LTS version (code). Here is the result:

Node.js 6.17.0 (indexOf is 3x faster): Array#indexOf x 7,221,953 ops/sec ±2.65% (79 runs sampled) Array#includes x 1,972,658 ops/sec ±2.02% (84 runs sampled) Fastest is Array#indexOf

Node.js 8.15.1 (same): Array#indexOf x 7,340,047 ops/sec ±4.30% (75 runs sampled) Array#includes x 8,899,792 ops/sec ±0.44% (90 runs sampled) Fastest is Array#includes

Node.js 10.15.3 (includes is 80x faster): Array#indexOf x 11,671,636 ops/sec ±4.92% (73 runs sampled) Array#includes x 861,062,120 ops/sec ±0.51% (91 runs sampled) Fastest is Array#includes

Node.js 11.11.0 (includes is 80x faster): Array#indexOf x 10,776,587 ops/sec ±4.10% (78 runs sampled) Array#includes x 854,403,715 ops/sec ±0.60% (88 runs sampled) Fastest is Array#includes

Quote from: lib: prefer using Array#includes instead of Array#indexOf · Issue #26568 · nodejs/node

That’s all from the Gemba.