Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
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
Test name
Executions per second
indexOf
28422.4 Ops/sec
map with forEach
1565.8 Ops/sec
map with loop
1336.7 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); }