Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Lodash union: Add items to unique array
(version: 0)
Union an array or 1 million items with an array of 2 million items, 1 million of which are duplicates.
Comparing performance of:
Set vs Object vs Lodash union
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var origNumItems = 1000000; // 1 million var newNumItems = origNumItems * 2; var origArr = []; var newItems = []; function initArray(arr, numItems) { for (var i = 0; i <= numItems; i++) { arr.push(`Item ${i}`); } } initArray(origArr, origNumItems); initArray(newItems, newNumItems);
Tests:
Set
var s = new Set(origArr); for (const i in newItems) { s.add(i); } Array.from(s);
Object
var obj = {}; for (const i in origArr) { obj[i] = true; } for (const i in newItems) { obj[i] = true; } _.keys(obj)
Lodash union
// Creating this duplicate array is needed to emulate creating the data structure that contains the new items, // which the other tests do by creating an object or set var newArr = []; for (const i in newItems) { newArr.push(i); } _.union(origArr, newArr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Object
Lodash union
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
1.6 Ops/sec
Object
1.8 Ops/sec
Lodash union
1.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition:** The benchmark measures the performance of three approaches to union an array with another array: 1. **Set**: Using JavaScript's built-in `Set` data structure to store unique values from the second array, and then converting it back to an array using `Array.from()`. 2. **Object**: Creating an object with keys set to values from both arrays, and then using Lodash's `_keys()` function to get an array of those keys. 3. **Lodash union**: Using Lodash's `union()` function to combine the two arrays into a single array with no duplicates. **Options Compared:** The three approaches compare different ways to handle duplicate values: * Set approach uses `Set` data structure, which automatically eliminates duplicates and has an efficient lookup time. * Object approach creates an object with keys set to values from both arrays, which can lead to slower performance due to the overhead of creating and looking up objects. However, it also provides more flexibility for further processing or manipulation of the data. * Lodash union approach uses a specialized function to combine the two arrays into a single array with no duplicates. **Pros and Cons:** * Set approach: + Pros: efficient lookup time, eliminates duplicates automatically + Cons: requires converting back to an array using `Array.from()`, which can be slower for large datasets * Object approach: + Pros: flexible for further processing or manipulation of the data, doesn't require conversion to an array + Cons: slower performance due to object creation and lookup overhead * Lodash union approach: + Pros: specialized function handles duplicates efficiently, doesn't require manual memory management + Cons: relies on external library (Lodash), which may add additional overhead **Library Used:** The test case uses Lodash version 4.17.15. Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object creation, and data transformation. **Special JS Feature/Syntax:** None mentioned in the benchmark definition. However, it's worth noting that using `_.keys()` function from Lodash requires importing the entire Lodash library, which may add additional overhead to the test results. **Alternatives:** Other approaches to union arrays could include: * Using a custom implementation with manual iteration and duplicate elimination * Utilizing a specialized data structure like a balanced binary search tree or a hash table * Leveraging modern JavaScript features like `Map` or `Set` constructors, which provide efficient lookup and insertion operations These alternatives might offer different trade-offs in terms of performance, memory usage, and code complexity compared to the Set, Object, and Lodash union approaches.
Related benchmarks:
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
lodash uniq vs spread new Set() medium size
lodash uniq vs set spread
Lodash _.union vs native Set() with large arrays
Lodash _.union vs native Set() with large arrays using spread
Comments
Confirm delete:
Do you really want to delete benchmark?