Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.get vs array.indexOf sorting v2
(version: 1)
Comparing performance of:
Map vs indexOf vs preparedMap
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const order = ['a', 'b', 'c'] const data = ['b', 'c', 'a', 'd'] const preparedMap = new Map(order.map((letter, index) => [letter, index]))
Tests:
Map
const orderMap = new Map(order.map((letter, index) => [letter, index])) data.sort((a, b) => (orderMap.get(a) ?? Infinity) - (orderMap.get(b) ?? Infinity))
indexOf
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 })
preparedMap
data.sort((a, b) => (preparedMap.get(a) ?? Infinity) - (preparedMap.get(b) ?? Infinity))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Map
indexOf
preparedMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 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
7013328.5 Ops/sec
indexOf
13768669.0 Ops/sec
preparedMap
10489057.0 Ops/sec
Related benchmarks:
Reduce vs Map+FromEntries vs for loop
MapOps
Map vs Object - in loop + data retrieval
Object vs Map creation (forEach, reduce, for)
test with letter
test with letter2
map initialisation
Map.get vs array.indexOf
Map.get vs array.indexOf sorting v2 with extra check
Comments
Confirm delete:
Do you really want to delete benchmark?