[JavaScript] Array.includes vs Array.indexOf Performance Comparison
I checked the execution times of JavaScript Array.includes and Array.indexOf using jsPerf.
I’ll introduce the results of investigating which is faster between JavaScript Array.includes and Array.indexOf.
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.