Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test map efficiency test2
(version: 1)
Comparing performance of:
Foreach test vs Map vs faster
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
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) { const excludes_map = new Map(); excludes.forEach(({k, v}) => { if (!excludes_map.has(k)) { excludes_map.set(k, new Set()) } excludes_map.get(k).add(v) }) return items.filter(item => ( excludes.every(({k}) => ( !(item.hasOwnProperty(k) && excludes_map.get(k).has(item[k])) )) )) } console.log(excludeItems(items, excludes))
faster
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) { let map = new Map(); let exclude_keys = new Set(); excludes.forEach(({k,v})=>{ exclude_keys.add(k); if(!map.has(k)){ map.set(k,new Set()); } map.get(k).add(v); }) return items.filter(item=>{ return [...exclude_keys].every(ex_key=>{ return !map.get(ex_key).has(item[ex_key]) }) }); } console.log(excludeItems(items, excludes))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Foreach test
Map
faster
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Comments
Confirm delete:
Do you really want to delete benchmark?