Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash UnionBy v Native Alternative
(version: 0)
Comparing performance of:
Lodash UnionBy vs Native UnionBy
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Lodash UnionBy
// Lodash var array1 = [2.1]; var array2 = [1.2, 2.3]; _.unionBy(array1, array2, Math.floor)
Native UnionBy
// Native var array1 = [2.1]; var array2 = [1.2, 2.3]; function unionBy(...arrays) { const iteratee = (arrays).pop(); if (Array.isArray(iteratee)) { return []; // return empty if iteratee is missing } return [...arrays].flat().filter( (set => (o) => set.has(iteratee(o)) ? false : set.add(iteratee(o)))(new Set()), ); }; unionBy(array1, array2, Math.floor);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash UnionBy
Native UnionBy
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 world of JavaScript microbenchmarks! **What is tested?** The provided JSON represents two benchmark tests for the `unionBy` function, which combines arrays while preserving duplicates and using a common element as a key for comparison. **Test Case 1: Lodash UnionBy** This test case uses the Lodash library. Lodash is a popular JavaScript utility library that provides various functions to manipulate data, among other things. The `unionBy` function in Lodash takes three arguments: * Two or more arrays * An optional iteratee function (a function that returns a value to be used for comparison) * A value to use as the "key" for combining elements In this specific test case, the `unionBy` function is called with two arrays (`array1` and `array2`) and the `Math.floor` function as the iteratee. **Pros and Cons of using Lodash UnionBy** Pros: * **Convenience**: Using a library like Lodash simplifies the implementation of complex functions like `unionBy`, making it easier to focus on other aspects of your project. * **Efficiency**: Lodash's optimized implementations can provide better performance compared to writing custom code. Cons: * **Dependency**: Your project now depends on the Lodash library, which might add an extra package size and complexity. * **Overhead**: Using a library means introducing additional overhead in terms of parsing, serialization, and other operations. **Test Case 2: Native UnionBy** This test case provides an alternative implementation for the `unionBy` function using native JavaScript features. The implementation uses the `flat()` method to flatten the arrays, the `filter()` method to remove duplicates, and a `Set` object to keep track of unique elements. Pros: * **No dependencies**: This implementation does not rely on any external libraries, making it more suitable for projects that require minimal dependencies. * **Efficient data structures**: Using `Sets` and `Arrays.flat()` can be more efficient than using Lodash's `Set` implementation. Cons: * **More complex code**: Writing the `unionBy` function from scratch requires a deeper understanding of JavaScript's built-in methods and data structures. * **Potential performance differences**: Depending on the specific use case, this native implementation might not outperform the Lodash version in terms of raw speed. **Other considerations** When writing your own implementation or using existing libraries like Lodash: * **Optimization techniques**: Look for ways to optimize your code, such as memoization, caching, and parallel execution. * **Performance metrics**: Understand the impact of various optimizations on performance, memory usage, and other aspects of your project. **Alternatives** If you want to explore alternative approaches or libraries: * **Other utility libraries**: Consider using alternatives like `lodash-es`, `ramda`, or `underscore`. * **Custom implementation**: Write your own implementation from scratch using native JavaScript features. * **Third-party implementations**: Explore third-party implementations, such as the one provided by the Mozilla Developer Network (MDN).
Related benchmarks:
_.union vs native Set() implementation
Lodash union vs native Set() implementation
Lodash union vs Native Javascript
Corrected Lodash _.union vs native Set()
Comments
Confirm delete:
Do you really want to delete benchmark?