Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.union VS. indexOf + push
(version: 0)
Comparing performance of:
_.union vs indexOf + push
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.17.11/lodash.min.js"></script>
Script Preparation code:
var arr = []; var add = _.range(100); add = add.concat(add);
Tests:
_.union
add.forEach(function (item) { arr = _.union(arr, [item]); });
indexOf + push
add.forEach(function (item) { if(arr.indexOf(item) === -1) { arr.push(item); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.union
indexOf + push
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 JSON data and explain what's being tested, compared, and some pros/cons of each approach. **Benchmark Definition** The benchmark is testing two approaches for merging arrays: using Lodash's `_.union` function versus concatenating an array and checking for duplicates using `indexOf` and `push`. **Script Preparation Code** ```javascript var arr = []; var add = _.range(100); add = add.concat(add); ``` This code creates an empty array `arr` and populates it with 100 elements using the `_range` function from Lodash. The `concat` method is used to double the size of the array, creating two identical arrays: `arr` and `add`. **Html Preparation Code** ```html <script src="https://cdn.jsdelivr.net/lodash/4.17.11/lodash.min.js"></script> ``` This line includes the Lodash library from a CDN. **Individual Test Cases** There are two test cases: 1. **_.union** ```javascript add.forEach(function (item) { arr = _.union(arr, [item]); }); ``` This code uses the `forEach` method to iterate over the elements of `add`. For each element, it calls the `_union` function from Lodash and merges the current array (`arr`) with a new array containing only the current element. The result is assigned back to `arr`. 2. **indexOf + push** ```javascript add.forEach(function (item) { if(arr.indexOf(item) === -1) { arr.push(item); } }); ``` This code uses the `forEach` method to iterate over the elements of `add`. For each element, it checks if the current array (`arr`) contains the element using `indexOf`. If not found, it pushes the element onto the end of the array. **What's being compared?** The benchmark is comparing the performance of these two approaches for merging arrays: * **_.union**: Using Lodash's `_.union` function to merge arrays. * **indexOf + push**: Checking if an element exists in the array using `indexOf` and pushing it onto the end if not found. **Pros and Cons** 1. **_.union** * Pros: + More concise and readable code + Lodash provides a robust implementation of union, which can handle many edge cases. * Cons: + May incur additional overhead due to function call and object creation. 2. **indexOf + push** * Pros: + Simple and straightforward code + Only relies on built-in JavaScript methods, reducing dependencies. * Cons: + More verbose code compared to _.union + Performance may degrade for large arrays due to repeated `indexOf` checks. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including array manipulation. In this benchmark, Lodash's `_union` function is used to merge the arrays. **Special JS feature/Syntax** None mentioned in the provided code. **Alternatives** If you need to perform array merging, consider using modern JavaScript methods like `set`, `add`, or ` concatAll` (if supported by your target browsers). Alternatively, you can implement your own union function using a combination of built-in methods like `indexOf` and `push`. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Array immutable union: lodash union vs flatten and creating a new set
Lodash Union vs Spread
Lodash Union vs Spread Operator
Lodash Union vs Spread with smaller arrays
Lodash Union vs Spread (100000)
Comments
Confirm delete:
Do you really want to delete benchmark?