Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Intersection with Arrays vs. native with sets vs. native filter with arrays
(version: 0)
Comparing performance of:
native arrays vs lodash intersection vs native with sets
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = []; for (let i = 0; i < 1000; i++) { arr1.push(i); } arr1 = _.shuffle(arr1); var arr2 = []; for (let i = 500; i < 1500; i++) { arr2.push(i); } arr2 = _.shuffle(arr2); set1 = new Set(arr1);
Tests:
native arrays
arr1.filter(item => arr2.indexOf(item) > -1);
lodash intersection
_.intersection(arr1, arr2);
native with sets
arr2.filter(item => set1.has(item));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
native arrays
lodash intersection
native with sets
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
native arrays
11938.8 Ops/sec
lodash intersection
12251.8 Ops/sec
native with sets
68793.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition JSON** The benchmark is defined by two parts: 1. **Script Preparation Code**: This code generates two large arrays (`arr1` and `arr2`) with 1000 and 500 elements, respectively, shuffled using the Lodash `shuffle()` function. These arrays are then used to create a new Set (`set1`) from `arr1`. 2. **Individual Test Cases**: There are three test cases: * `native arrays`: This test case uses native JavaScript's `filter()` method on `arr1` with an iterator that checks if each element is present in `arr2`. * `lodash intersection`: This test case uses the Lodash library's `intersection()` function to find the common elements between `arr1` and `arr2`. * `native with sets`: This test case uses native JavaScript's `filter()` method on `arr2` with an iterator that checks if each element is present in the Set (`set1`) created from `arr1`. **Options Compared** The three test cases are comparing different approaches to find common elements between two arrays: 1. **Native Arrays**: Uses native JavaScript's `filter()` method and an iterator that checks for presence in another array. 2. **Lodash Intersection**: Uses the Lodash library's `intersection()` function, which is a higher-order function that takes multiple arguments (arrays) and returns their intersection. 3. **Native with Sets**: Uses native JavaScript's `filter()` method and an iterator that checks for presence in a Set created from another array. **Pros and Cons of Each Approach** 1. **Native Arrays**: * Pros: Fast and lightweight, uses built-in functions. * Cons: Can be slow if the arrays are very large, as it needs to iterate over the entire array. 2. **Lodash Intersection**: * Pros: Convenient and concise, doesn't require manual iteration. * Cons: Requires the Lodash library to be included in the test environment, which may add overhead. 3. **Native with Sets**: * Pros: Can be faster for large datasets, as Set lookups are O(1) on average. * Cons: Requires creating a new Set from one of the arrays, which can be slower than native array operations. **Library and Purpose** The Lodash library is a popular utility library that provides various functions to simplify common tasks. In this case, the `intersection()` function is used to find the common elements between two arrays. **Special JS Feature or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The tests only use standard JavaScript features like arrays, Sets, and the `filter()` method. **Other Alternatives** If you wanted to implement a similar benchmark without using the Lodash library, you could consider alternative libraries like `lodash-es` (a lighter version of Lodash) or implementing your own intersection function from scratch. Alternatively, you could also use native JavaScript's built-in functions like `Set.prototype.has()` and `Array.prototype.some()` to achieve the same result. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
Intersection filter vs lodash intersection test unsorted array
native intersect vs lodash intersection
Lodash difference vs filtering via set membership with high overlap
Array Intersection vs. Set Intersection vs. Lodash - big
Comments
Confirm delete:
Do you really want to delete benchmark?