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
gemma2:9b
, generated one year ago):
This benchmark tests different approaches to finding the union of two arrays: `array1` and `array2`. The goal is to identify which method performs best in terms of execution speed. Let's break down each test case: **1. `Array.includes - reduce`:** * **Method:** This approach uses JavaScript's `reduce` method to iterate over a set containing all unique elements from both arrays. For each element, it checks if it exists in both `array1` and `array2`, categorizing it accordingly (union, `array1` only, or `array2` only). * **Pros:** Potentially efficient if the set operations are optimized by JavaScript's engine. * **Cons:** Can be less performant than other methods if `reduce` has a higher overhead. **2. `Array.includes - for of`:** * **Method:** This uses a `for...of` loop to iterate over the flattened array (`[...array1, ...array2]`). For each element, it checks its presence in both original arrays, categorizing it like before. * **Pros:** Simpler to read and understand compared to `reduce`. * **Cons:** Potentially less efficient than `Map.has` or `Set.has` as it involves linear iteration through a potentially larger array. **3. `Map.has`:** * **Method:** Creates two `Map` objects, one for each array. Then, it iterates over the combined keys of both maps and checks if an element exists in both maps using the `has()` method. * **Pros:** Maps generally offer efficient key lookups (O(1) average complexity), making this approach potentially very fast. * **Cons:** Requires more memory to store the maps. **4. `Set.has`:** * **Method:** Creates two `Set` objects, one for each array. Similar to the `Map.has` approach, it iterates over the combined set and checks for element existence using `has()`. * **Pros:** Sets also offer efficient membership testing (O(1) average complexity). Can be slightly faster than maps due to a potentially smaller memory footprint. * **Cons:** Requires more memory compared to using arrays directly. **Benchmark Results:** The results show that the `Map.has` method is the fastest, followed closely by `Set.has`. The `Array.includes` methods are slower, particularly `Array.includes - reduce`. Let me know if you have any other questions or would like a deeper dive into a specific aspect!
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?