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:
10 months ago
by:
Guest
Go 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:
26 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0
Browser/OS:
Firefox 152 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
indexOf
1.2M/s
Map
1.1M/s
View exact numbers
Test name
Executions per second
✓
indexOf
1,182,777 Ops/sec
Map
1,134,638 Ops/sec
Related benchmarks
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?