Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Union Lodash Set ObjectBased
(version: 3)
Comparing performance of:
Lodash vs ObjectBased vs Spread
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var a = [], b = [], c = [], d = [], i; for (i = 0; i < 1000; i++) { a.push(Math.floor(Math.random() * 1000) + 1); b.push(Math.floor(Math.random() * 1000) + 1); } var union = (function(){ return function(){ let ob={}; let arr = [...arguments]; let len = arr.length; for(let i=0;i<len;i++) arr[i].forEach(num => ob[num] = 1); return Object.keys(ob); } }());
Tests:
Lodash
_.union(a,b)
ObjectBased
union(a,b)
Spread
[...new Set([...a,...b])];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
ObjectBased
Spread
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 explanation of the benchmark. **Benchmark Definition JSON** The provided JSON defines two benchmarks: "Union Lodash Set ObjectBased" and its variants. We'll focus on the first one. **What is tested?** The "Union Lodash Set ObjectBased" benchmark tests the performance of three different approaches to calculate the union of two arrays: 1. **Lodash**: The benchmark uses the `_union` function from the Lodash library, which takes multiple arguments and returns an array containing unique elements. 2. **Object-based approach**: This is a custom implementation that creates an object with numerical keys (values from the input arrays) and then returns the keys of this object as an array. This approach has two variants: * **`union(a,b)`** : Directly calls the `union` function defined in the benchmark script. * **`[...new Set([...a,...b])];`**: Uses the spread operator to concatenate the input arrays, then converts the result to a set using the `Set` constructor and the spread syntax `[...]`. 3. **Spread approach**: This is another custom implementation that uses the spread operator to concatenate the input arrays, then converts the result to an array using the `Array.from()` method. **Options compared** The benchmark compares the performance of these three approaches: * Lodash: `_union` function * Object-based approach: + `union(a,b)` + `[...new Set([...a,...b])];` * Spread approach: `[...new Set([...a,...b])];` **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Lodash**: Pros: * Well-maintained and widely-used library. * High-performance implementation. Cons: * Requires an external dependency (the Lodash library). 2. **Object-based approach: `union(a,b)`**: Pros: + Custom implementation, potentially optimized for performance. Cons: + More verbose code compared to the spread approach. 3. **Spread approach: `[...new Set([...a,...b])];`**: Pros: + Concise and easy-to-read code. Cons: + May have higher overhead due to the use of `Array.from()` and `Set`. 4. **Object-based approach: `[...new Set([...a,...b])];`**: Pros: + Combination of both approaches, leveraging the spread operator for conciseness. Cons: + Similar trade-offs between performance and readability as the spread approach. **Library and syntax** In this benchmark, we see the use of: * Lodash library: `_union` function * Custom JavaScript features/syntax: + Spread operator (`...`) + Set constructor (`Set()`) These features are widely used in modern JavaScript development, but may not be familiar to all developers. **Alternatives** Other approaches to calculating the union of two arrays might include: * Using `Array.prototype.concat()`, `Array.prototype.reduce()`, or other array methods. * Implementing a custom merge function using loops and conditional statements. * Leveraging libraries like Underscore.js or Ramda, which provide optimized implementations of union functions. Keep in mind that the performance of these alternatives might differ from the ones tested in this benchmark.
Related benchmarks:
test124578
Array immutable union: lodash union vs object.assign vs uniq
Lodash vs. Native Union
Lodash.js vs Native values
Lodash _.union vs native Set() with large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?