Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
indexOf vs map
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser:
Chrome 120
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Benchmark engine:
Benchmark.js
indexOf
28K/s
map with forEach
2K/s
map with loop
1K/s
View exact numbers
Test name
Executions per second
✓
indexOf
28,422 Ops/sec
map with forEach
1,566 Ops/sec
map with loop
1,337 Ops/sec
Script Preparation code:
function hasWithIndexOf(needle, haystack) { return haystack.indexOf(needle) !== -1; } function hasWithMapForEach(needle, haystack) { var map = {}; haystack.forEach(item => map[item] = true); return map[needle]; } function hasWithMapLoop(needle, haystack) { var map = {}; for (var i=0, len=haystack.length; i<len; ++i) { map[haystack[i]] = true; } return map[needle]; } var testArray = []; for (var i=0; i<100; ++i) { testArray.push('00' + i); }
Tests:
indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404', testArray); }
map with forEach
for (var i=0; i<100; ++i) { hasWithMapForEach('404', testArray); }
map with loop
for (var i=0; i<100; ++i) { hasWithMapLoop('404', testArray); }