Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filter & includes vs Lodash intersection
(version: 0)
Comparing performance of:
Native vs Lodash
Created:
one year 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.includes(item));
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
9981.4 Ops/sec
Lodash
27222.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare the performance of native JavaScript `filter` method versus Lodash's `intersection` function on two arrays, `arr1` and `arr2`. The test case uses a random shuffling algorithm to ensure the input data is unpredictable. **Array Preparation Code** The preparation code creates two large arrays: ```javascript 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); ``` The first array `arr1` has 1000 elements, and the second array `arr2` has 1000 elements as well. The `_.shuffle` function from Lodash is used to randomize the order of elements in both arrays. **Benchmark Definition** There are two benchmark definitions: 1. **Native**: `arr1.filter(item => arr2.includes(item));` This code uses the native JavaScript `filter` method to find elements present in both `arr1` and `arr2`. 2. **Lodash**: `_.intersection(arr1, arr2);` This code uses Lodash's `intersection` function to find elements common to both `arr1` and `arr2`. **Comparison** The benchmark is designed to compare the performance of these two approaches: * Native JavaScript: Uses built-in methods (`filter`, `includes`) without any external dependencies. * Lodash: Uses an external library (Lodash) with its own implementation of `intersection`. **Pros and Cons** **Native JavaScript:** Pros: * No external dependencies, reducing overhead. * Native performance optimizations. Cons: * Requires manual implementation of filtering logic, which can be complex. * May not handle edge cases or special scenarios as efficiently as Lodash. **Lodash:** Pros: * Provides a simple and efficient way to perform set operations. * Handles edge cases and special scenarios out-of-the-box. * Reduces boilerplate code and improves readability. Cons: * Requires an external dependency (Lodash), increasing overhead. * May introduce additional complexity due to the library's implementation. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including array operations. The `intersection` function in particular is designed to efficiently find common elements between two arrays. **Special JS Feature or Syntax: None** There are no special features or syntax used in this benchmark. Both approaches rely on standard JavaScript functionality. **Alternative Approaches** Other alternatives to the native JavaScript `filter` method and Lodash's `intersection` function include: * Using other array operations, such as `every`, `some`, or `find`. * Implementing a custom set intersection algorithm using bitwise operations. * Utilizing specialized libraries like Fastest or Array-Union for efficient set operations. Keep in mind that the choice of approach depends on specific requirements and performance constraints.
Related benchmarks:
Array filter vs Lodash intersection
Intersection filter vs lodash intersection test unsorted array
lodash intersectionBy vs array filtering with includes
native intersect vs lodash intersection
Lodash Intersection with Arrays vs. native with sets vs. native filter with arrays
Comments
Confirm delete:
Do you really want to delete benchmark?