Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash intersection test
(version: 0)
Comparing performance of:
Lodash vs Map
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
Script Preparation code:
var arr1 = []; for (var i = 0; i <= 100; i++) { arr1.push({ id: i, text: 'blabla-' + i }); } var arr2 = []; for (var i = 0; i <= 150; i+=2) { arr2.push({ id: i, text: 'blabla-' + i }); }
Tests:
Lodash
_.intersection(arr1, arr2);
Map
const m = new Map(arr2.map(i => [i.id, i])); arr1.filter(a => m.has(a.id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Map
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net! **Overview** The benchmark consists of two test cases: Lodash and Map. The purpose of these tests is to compare the performance of two approaches for finding common elements between two arrays. **Test Case 1: Lodash** The first test case uses the `lodash.intersection()` function, which returns a new array with only the elements that are present in both input arrays. In this case, we have two arrays, `arr1` and `arr2`, where `arr1` has 101 elements (with unique IDs) and `arr2` has 75 elements (with incrementing IDs). The test code is: ```javascript _.intersection(arr1, arr2); ``` **Pros:** 1. Concise and easy to read. 2. No manual iteration required. **Cons:** 1. External dependency on the Lodash library (which may not be included in all environments). 2. May incur overhead due to the need for a function call. **Test Case 2: Map** The second test case uses a more low-level approach, utilizing JavaScript's built-in `Map` data structure and array methods. ```javascript const m = new Map(arr2.map(i => [i.id, i])); arr1.filter(a => m.has(a.id)); ``` **Pros:** 1. No external dependencies. 2. Utilizes built-in JavaScript features. **Cons:** 1. More verbose than the Lodash approach. 2. Requires manual iteration and array manipulation. **Library and Features** In this benchmark, two libraries are used: 1. **Lodash**: A utility library that provides a `intersection()` function for working with arrays. The `lodash.min.js` file is included via the `<script>` tag in the HTML preparation code. 2. **Map**: JavaScript's built-in `Map` data structure is used to create a lookup table of elements from `arr2`. This allows us to efficiently check if an element is present in both arrays. No special JavaScript features or syntax are required for these tests. **Alternatives** Other approaches that could be taken include: 1. **Use the `filter()` method on one of the arrays**, like this: `[...arr1].filter(a => arr2.includes(a))`. This would eliminate the need for a separate lookup table, but may incur overhead due to the need for array creation and filtering. 2. **Use a custom implementation using bitwise operations** or other low-level techniques. These approaches could potentially be faster than the Lodash approach, but are likely more complex and error-prone. Overall, the benchmark highlights the trade-offs between convenience, conciseness, and performance when working with JavaScript arrays and data structures.
Related benchmarks:
Lodash: difference vs intersection
Replace _.intersection
Lodash vs. Native
Intersection filter vs lodash intersection
Lodash vs. Set Intersection (1000000)
Comments
Confirm delete:
Do you really want to delete benchmark?