Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Lodash union: Union arrays of strings
(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
_.union(origArr, newItems)
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the details of the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three different approaches for unionizing two arrays: using a `Set` data structure, an object literal (`Object`), and the `_.union()` function from the Lodash library. The test case involves creating two large arrays with 1 million and 2 million items, respectively, where 1 million of the items are duplicates. **Tested Options** The three options being compared are: 1. **Set**: Using a `Set` data structure to unionize the arrays. * Pros: Fast lookup times, efficient memory usage. * Cons: May not be suitable for complex or nested data structures. 2. **Object Literal**: Using an object literal (`{}`) to unionize the arrays by iterating over both arrays and adding each item as a property of the object. * Pros: Easy to understand, flexible for complex data structures. * Cons: May be slower than `Set` or Lodash due to additional overhead. 3. **Lodash Union**: Using the `_.union()` function from the Lodash library to unionize the arrays. * Pros: Fast and efficient, suitable for large datasets. * Cons: Requires an external dependency (Lodash). **Library and Purpose** The test case uses the Lodash library, which is a popular JavaScript utility library that provides a wide range of functions for working with data structures, such as arrays, objects, and sets. **Special JS Features/Syntax** None mentioned in this benchmark. **Benchmark Preparation Code** The script preparation code initializes two large arrays (`origArr` and `newItems`) with 1 million and 2 million items, respectively. The `initArray()` function is used to populate the arrays with unique item strings. This code serves as a common setup for all three test cases. **Individual Test Cases** Each test case includes a benchmark definition (JavaScript code) that performs the unionization operation using one of the three options: 1. **Set**: Uses a `Set` data structure to iterate over both arrays and add each item. 2. **Object Literal**: Iterates over both arrays and adds each item as a property of an object literal. 3. **Lodash Union**: Uses the `_.union()` function from Lodash to perform the unionization operation. **Benchmark Results** The latest benchmark result shows the execution times per second for each test case on Chrome 91, running on Windows. The results indicate that: 1. Lodash Union is the fastest option. 2. Set is the next-fastest option. 3. Object Literal is slower than both `Set` and Lodash Union. **Other Alternatives** If you're interested in exploring alternative approaches for unionizing arrays, some other options include: * Using a `Map` data structure instead of `Set`. * Utilizing a custom implementation using JavaScript's built-in array methods (e.g., `filter()`, `reduce()`). * Leveraging other libraries or frameworks that provide efficient unionization functions (e.g., Angular's `UnionArray`). Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
Lodash union vs Native Javascript
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
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?