Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash intersection test vs Set/Map
(version: 0)
Comparing performance of:
Lodash vs Map/Set
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/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
_.intersectionWith(arr1, arr2, (a,b) => a.id == b.id);
Map/Set
const s = new Set(arr2); arr1.filter(a => s.has(a));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Map/Set
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
102214.0 Ops/sec
Map/Set
609850.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and their pros and cons. **Benchmark Purpose** The benchmark tests two approaches to find the intersection of two arrays: 1. **Lodash Intersection**: Using the `_.intersectionWith` function from the Lodash library. 2. **Map/Set Approach**: Using a Set data structure to store the second array and then filtering the first array against the set. **Options Compared** The benchmark compares the performance of these two approaches on two arrays: * `arr1`: an array with 101 elements, each containing an `id` property. * `arr2`: an array with 75 elements (every other element starting from 0), each containing an `id` property. **Pros and Cons** ### Lodash Intersection Pros: * Easy to implement and use, as it's a well-known library function. * Can handle complex intersection logic using the provided callback function. Cons: * May have overhead due to the library's functionality, such as error handling and optimization. * The performance may be affected by the number of iterations in the callback function. ### Map/Set Approach Pros: * Generally faster for large datasets, as it leverages the efficient Set data structure operations. * Can be more memory-efficient than using a library like Lodash. Cons: * Requires manual implementation of the intersection logic. * May not be as convenient or readable for complex intersection scenarios. **Library and Syntax** The `_.intersectionWith` function from Lodash is used to perform the intersection. This function takes three arguments: the first array, the second array, and a callback function that defines how elements are compared. In this benchmark, the callback function simply checks if the `id` properties of two objects are equal (`a.id == b.id`). This is a simple comparison and can be optimized further for larger datasets. **Other Considerations** The benchmark uses Chrome 120 as the browser and runs on a Linux desktop. The test results are reported in executions per second, which provides an indication of performance. In terms of alternatives, other approaches to find array intersections include: * Using `Array.prototype.filter()` with a callback function, similar to the Map/Set approach. * Implementing a custom algorithm using bitwise operations or arithmetic. * Utilizing specialized libraries like FastestFind or ArrayUtils. However, these alternatives may not be as efficient or convenient as using the Lodash library or the Set data structure for large datasets.
Related benchmarks:
Lodash vs. Native
Array Intersection vs. Set Intersection vs. Lodash
Array Intersection vs. Set Intersection vs. Lodash - big
Array Intersection vs. Set Intersection vs. Lodash part 5
Array Intersection vs. Set Intersection vs. Lodash part 3 mix
Comments
Confirm delete:
Do you really want to delete benchmark?