Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filter vs Lodash intersection
(version: 0)
Comparing performance of:
Native vs Lodash
Created:
6 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);
Tests:
Native
arr1.filter(item => arr2.indexOf(item) > -1);
Lodash
_.intersection(arr1, arr2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
10569.1 Ops/sec
Lodash
17649.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to find common elements between two arrays: 1. **Native**: Using the `filter` method with an arrow function that checks if an element is present in the second array using `indexOf`. 2. **Lodash**: Using the `intersection` function from the Lodash library, which takes two arrays as input and returns a new array containing only the elements that are common to both. **Options Compared** The benchmark is comparing these two approaches: * **Native**: Using the built-in `filter` method with an arrow function. * **Lodash**: Using the `intersection` function from Lodash library. **Pros and Cons of Each Approach** 1. **Native**: * Pros: No additional dependency (Lodash), potentially faster execution since it's a native JavaScript method. * Cons: May be slower due to the overhead of calling an external library, especially for smaller arrays. 2. **Lodash**: * Pros: Potentially faster execution since Lodash is optimized for performance, comes with a larger community and more extensive documentation. * Cons: Requires including the Lodash library, which may increase bundle size. **Other Considerations** The benchmark also considers the impact of array shuffling on performance. The script preparation code shuffles both arrays using `_.shuffle(arr1)` and `arr2 = _.shuffle(arr2)`, which ensures that the arrays are randomly distributed before running the benchmarks. This helps to mitigate any biases in the results due to array ordering or distribution. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string processing, and more. The `intersection` function is part of Lodash's "More Utilities" module and takes two arrays as input and returns a new array containing only the elements that are common to both. **Special JS Feature/Syntax** There isn't any specific JavaScript feature or syntax being tested in this benchmark. However, it's worth noting that arrow functions (e.g., `item => arr2.indexOf(item) > -1`) are used in both benchmarks, which is a relatively modern JavaScript feature introduced in ECMAScript 2015. **Alternatives** If you're looking for alternative approaches to find common elements between two arrays, you could consider: * Using the `set` data structure and set intersection operations. * Utilizing libraries like FastUtils or Array-Intersect. * Implementing a custom solution using bitwise operations and array iteration.
Related benchmarks:
Intersection filter vs lodash intersection test unsorted array
Array filter vs Lodash intersection 1
Array filter vs Lodash intersection 2
Lodash Intersection with Arrays vs. native with sets vs. native filter with arrays
Comments
Confirm delete:
Do you really want to delete benchmark?