Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test map efficiency
(version: 1)
Comparing performance of:
Foreach test vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
Foreach test
let items = [ {color: 'red', type: 'tv', age: 18}, {color: 'silver', type: 'phone', age: 20}, {color: 'blue', type: 'book', age: 17} ] const excludes = [ {k: 'color', v: 'silver'}, {k: 'type', v: 'tv'}, ] function excludeItems(items, excludes) { excludes.forEach( pair => { items = items.filter(item => item[pair.k] === item[pair.v]) }) return items } console.log(excludeItems(items, excludes))
Map
let items = [ {color: 'red', type: 'tv', age: 18}, {color: 'silver', type: 'phone', age: 20}, {color: 'blue', type: 'book', age: 17} ] const excludes = [ {k: 'color', v: 'silver'}, {k: 'type', v: 'tv'}, ] function excludeItems(items, excludes) { // m k n // n * m // change exclude to Map<key, Set<value>> // m * k * 1 // preprocess excludes array // avoid multiple for loop on items const excludeMap = new Map() for (let {k, v} of excludes) { if (!excludeMap.has(k)) { excludeMap.set(k, new Set()) } excludeMap.get(k).add(v) } return items.filter(item => { return Object.keys(item).every( key => !excludeMap.has(key) || !excludeMap.get(key).has(item[key]) ) }) } console.log(excludeItems(items, excludes))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Foreach test
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Foreach test
475833.9 Ops/sec
Map
183514.7 Ops/sec
Related benchmarks:
Filtering test
Get min value from array of objects' value
not includes vs. not equal to
for+if vs filter
Test Map Number Split 2
lodash difference vs reduce
JS Filter vs Reduce vs Foreach
my map vs for loop
Trey - unique array
Comments
Confirm delete:
Do you really want to delete benchmark?