Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
union and native , add item to array without duple
(version: 0)
Comparing performance of:
_.union vs native
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Script Preparation code:
var a = [1, 2, 3, 4, 5] var b = 3
Tests:
_.union
var c = _.union(a, [b])
native
if(!a.includes(b)){ a.push(b); } var c = a;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.union
native
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 break down the provided benchmark and explain what's being tested, compared options, pros/cons, and other considerations. **Benchmark Overview** The benchmark is comparing two approaches: 1. Using the `_` object from Lodash (a utility library) to union an array (`_.union`) with a new element. 2. Implementing a native JavaScript solution without using any libraries, specifically checking if the array already includes the new element before adding it. **Lodash Library** The `_.union()` function is a utility function in Lodash that combines two or more arrays into a single array, removing duplicates. The purpose of this library is to provide a convenient and efficient way to perform common array operations. **Native JavaScript Solution** The native solution uses the `includes()` method to check if the new element (`b`) is already present in the array (`a`). If not, it adds the new element using the `push()` method. **Comparison Options** There are two options being compared: 1. **_.union()**: Uses Lodash's `_` object and its `_.union()` function. * Pros: + Efficient way to union arrays without having to iterate through them manually. + Provides a standardized, tested solution (Lodash is widely used). * Cons: + Adds an external dependency (Lodash), which might not be desirable for all use cases. + Might not perform as well as the native solution if the array is very large. 2. **Native**: Implements the union operation manually using JavaScript's built-in methods (`includes()` and `push()`). * Pros: + No external dependencies, making it a self-contained solution. + Can potentially be faster or more efficient for very large arrays. **Other Considerations** * The benchmark is running on desktop platforms (Chrome 83) and Windows operating systems. If you want to test the same benchmark on different platforms or browsers, you would need to modify the `Html Preparation Code` accordingly. * The benchmark is measuring the number of executions per second (`ExecutionsPerSecond`) for each test case. This metric provides an idea of how efficient each approach is in terms of performance. **Alternatives** If you want to explore other approaches or alternatives, here are a few options: 1. **Using `Set`**: You can use JavaScript's built-in `Set` object to union arrays. However, this would require iterating through the array and adding elements to the set. 2. **Iterating manually**: Another approach is to iterate through both arrays and add unique elements to a new array. This method would be similar to the native solution but might not be as efficient. Keep in mind that these alternatives are not necessarily better or worse than the options being compared, but rather provide additional perspectives on how to achieve the same goal (unioning arrays).
Related benchmarks:
Array immutable union: lodash union vs flatten and creating a new set
Lodash _.union vs native Set() and Set to Array
Lodash union vs Native Javascript
lodash union vs native set spread
Comments
Confirm delete:
Do you really want to delete benchmark?