Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findUnion - nested Array.includes vs Map.has vs Set.has
(version: 0)
Comparing performance of:
Array.includes - reduce vs Array.includes - for of vs Map.has vs Set.has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var empty = { id: '', number: 10000 }; var array1 = [empty]; var array2 = [empty]; var entities = { '': empty }; var id = ''; for (let i = 0; i < 1500; i++) { id = String(i); entities[id] = { id, number: i } } function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive } for (let i = 0; i < 500; i++) { array1.push(entities[getRandomInt(i, 1500)]); } for (let i = 0; i < 500; i++) { array2.push(entities[getRandomInt(i, 1500)]); }
Tests:
Array.includes - reduce
var data = Array.from(new Set([...array1, ...array2])).reduce( (acc, item) => { if (array1.includes(item)) { if (array2.includes(item)) { acc.union.push(item); } else { acc.lhsOnly.push(item); } } else { acc.rhsOnly.push(item); } return acc; }, { rhsOnly: [], lhsOnly: [], union: [] } ); console.log(data)
Array.includes - for of
var unionArray = Array.from(new Set([...array1, ...array2])); var data = { rhsOnly: [], lhsOnly: [], union: [] } for (var item of unionArray) { if (array1.includes(item)) { if (array2.includes(item)) { data.union.push(item); } else { data.lhsOnly.push(item); } } else { data.rhsOnly.push(item); } } console.log(data)
Map.has
var lMap = new Map(array1.map((item) => [item, true])); var rMap = new Map(array2.map((item) => [item, true])); var unionSet = new Set([...lMap.values(), ...rMap.values()]); var result = { rhsOnly: [], lhsOnly: [], union: [] }; for (var item of unionSet) { if (lMap.has(item)) { if (rMap.has(item)) { result.union.push(item); } else { result.lhsOnly.push(item); } } else { result.rhsOnly.push(item); } } console.log(result)
Set.has
var lSet = new Set(array1); var rSet = new Set(array2); var unionSet = new Set([...lSet.values(), ...rSet.values()]); var result = { rhsOnly: [], lhsOnly: [], union: [] }; for (var item of unionSet) { if (lSet.has(item)) { if (rSet.has(item)) { result.union.push(item); } else { result.lhsOnly.push(item); } } else { result.rhsOnly.push(item); } } console.log(result)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.includes - reduce
Array.includes - for of
Map.has
Set.has
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!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what this benchmark is testing and the pros/cons of each approach. **Benchmark Description** The benchmark tests how efficiently different data structures can be used to find the union (common elements) between two arrays, `array1` and `array2`. The test cases cover three approaches: using `Array.includes`, `Map.has`, and `Set.has`. **Test Cases** ### Array.includes - reduce * **Method**: Uses `Array.reduce` with `Array.includes` to find the union. * **Script**: Iterates through a union array created from `array1` and `array2`, checks if an element is in both arrays using `includes`, and categorizes it as either "lhsOnly", "rhsOnly", or "union". * **Pros**: Simple, easy to understand, and uses built-in Array methods. * **Cons**: Slow performance due to the nested loop and array manipulation. ### Array.includes - for of * **Method**: Uses a `for...of` loop with `Array.includes` to find the union. * **Script**: Similar to the reduce approach but uses a simple loop instead. * **Pros**: Easy to understand, similar to the previous method, but slightly faster due to fewer function calls. * **Cons**: Slow performance due to the loop and array manipulation. ### Map.has * **Method**: Uses two `Map` objects (`lMap` and `rMap`) with `has` to find the union. * **Script**: Creates two maps from `array1` and `array2`, finds the union of their values, and then categorizes each item as either "lhsOnly", "rhsOnly", or "union". * **Pros**: Fast performance due to map lookup times (average O(1) complexity). * **Cons**: May have higher memory usage compared to sets. ### Set.has * **Method**: Uses two `Set` objects (`lSet` and `rSet`) with `has` to find the union. * **Script**: Similar to the map approach but uses sets instead. * **Pros**: Fast performance due to set lookup times (average O(1) complexity). * **Cons**: May have higher memory usage compared to maps. **Latest Benchmark Results** The latest results show: | Test Name | Executions Per Second | | --- | --- | | Map.has | 11,667.24 | | Set.has | 8,880.97 | | Array.includes - reduce | 2,707.35 | | Array.includes - for of | 2,696.55 | **Conclusion** The `Map` and `Set` approaches are the clear winners in terms of performance, with `Map.has` being slightly faster than `Set.has`. The `Array.includes` methods, while simple to understand, have slower performance due to their iterative nature. If you need to find unions between two arrays frequently, using `Map` or `Set` would be a good choice for optimized performance.
Related benchmarks:
Math.max vs Array.reduce
Math.max with nested slice vs iife tracking max.
Get max from an array of numbers (Math.max vs. iteration)
Get max from an array of numbers (Math.max vs. iteration) V2
Finding the max value of a property in an array of objects
Comments
Confirm delete:
Do you really want to delete benchmark?