Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs Set
(version: 0)
Comparing performance of:
Lodash union vs Js set vs custom union
Created:
one year ago
by:
Guest
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 = ['a', 'b', 'c']; var b = ['b', 'd', 'a', 'e', 'f']; var customUnion = (x, y) => { const existSet = new Set(x) const res = [...x] y.forEach((z) => {if (!existSet.has(z)) { res.push(z) }}) return res }
Tests:
Lodash union
var c = _.union(a, b).join(' ');
Js set
var c = [...new Set([...a ,...b])].join(' ');
custom union
var c = customUnion(a,b).join(' ')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash union
Js set
custom union
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:129.0) Gecko/129.0 Firefox/129.0
Browser/OS:
Firefox Mobile 129 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash union
1187672.1 Ops/sec
Js set
403669.3 Ops/sec
custom union
773478.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. The benchmark is comparing three approaches to union (merge) two arrays: 1. **Lodash's `_.union()` method**: This function takes two arrays as input and returns a new array containing all unique elements from both arrays. The implementation used here is the 4.17.5 version of Lodash. 2. **JavaScript's built-in `Set` data structure**: A Set is an object that stores unique values, which can be used to eliminate duplicates when merging two arrays. This approach uses the spread operator (`...`) to create a new array from the elements in one of the input arrays, and then iterates over the other array to add any new elements. 3. **Custom union function**: A custom JavaScript function defined by the user, which achieves the same goal as Lodash's `_.union()` method but with a different implementation. Now, let's discuss the pros and cons of each approach: * **Lodash's `_.union()` method**: + Pros: Well-tested and widely used, so you can rely on its performance and behavior. + Cons: Additional dependency on Lodash, which might add overhead to your project. Also, this implementation uses a proprietary algorithm that might not be easily understandable or optimized by others. * **JavaScript's built-in `Set` data structure**: + Pros: Native support, so it doesn't require any additional dependencies. The implementation is relatively simple and easy to understand. Additionally, you can use other Set-related methods (e.g., `_.union()` alternatives like `Array.from(set).concat()`) to achieve similar results. + Cons: Might be slower than Lodash's optimized implementation due to the overhead of creating a new array from the Set. * **Custom union function**: + Pros: Gives you complete control over the implementation and optimization. You can tailor it to your specific use case or performance requirements. + Cons: Requires more effort and expertise to implement correctly, as it's not a standard library function. The custom union function is an interesting approach, but it also introduces the following considerations: * **Performance**: While you have fine-grained control over the implementation, this can sometimes result in slower performance compared to optimized libraries like Lodash. * **Code readability and maintainability**: With a custom implementation, others might struggle to understand or update the code, especially if it's complex or uses non-standard techniques. In general, for most use cases, JavaScript's built-in `Set` data structure is a good choice due to its simplicity, performance, and widespread adoption. However, if you have specific requirements that necessitate a custom implementation, the pros of this approach outweigh the cons. As for other alternatives, some options could include: * Using a different library or implementation, such as `lodash-es` (a ES6-compatible version of Lodash) or `ramda`. * Implementing a union function using a different data structure, like an array with duplicate checks. * Utilizing modern JavaScript features like `Array.prototype.filter()` and `Set.prototype.add()` to create the union. Keep in mind that each alternative has its own trade-offs and requirements.
Related benchmarks:
Lodash union vs native Set() implementation
Spread Set vs Lodash uniq
Array immutable union: lodash union vs flatten and creating a new set
Array immutable union: set from lodash union vs set from lodash flatten
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?