Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.get vs array.indexOf
(version: 1)
Comparing performance of:
Map vs indexOf
Created:
8 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
Map
const order = ['a', 'b', 'c'] const orderMap = new Map(order.map((letter, index) => [letter, index])) const data = ['b', 'c', 'a', 'd'] data.sort((a, b) => (orderMap.get(a) ?? Infinity - orderMap.get(b) ?? Infinity))
indexOf
const order = ['a', 'b', 'c'] const data = ['b', 'c', 'a', 'd'] data.sort((a, b) => { const indexA = order?.indexOf(a) const indexB = order?.indexOf(b) if (indexA !== -1 && indexB !== -1) return indexA - indexB if (indexA === -1 && indexB !== -1) return 1 if (indexA !== -1 && indexB === -1) return -1 return 0 })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
7097903.5 Ops/sec
indexOf
10861959.0 Ops/sec
Related benchmarks:
sort test 3
spread vs array.from on Map sorted
tickets
array vs onject
Test Sorting Locations etc etc etc etc
Math.max() vs .map().sort().reverse()
math.min/max vs sort
Map.get vs array.indexOf sorting v2
Map.get vs array.indexOf sorting v2 with extra check
Comments
Confirm delete:
Do you really want to delete benchmark?