Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check speed of lodash intersection vs custom
(version: 0)
Comparing performance of:
lodash vs sort
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.5/lodash.min.js'></script>
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; var arr2 = []; for(var i = 0; i < 51; i++){ arr.push(getRandomInt(100)); arr2.push(getRandomInt(100)); }
Tests:
lodash
_.intersection(arr,arr2);
sort
var getArrayIntersection = (a, b) => { var setA = new Set(a), setB = new Set(b); var comparator = { array: setA, set: setB, }; if (setA.size > setB.size) { comparator.array = setB; comparator.set = setA; } const intersection = [...comparator.array].filter((el) => comparator.set.has(el), ); return intersection; }; getArrayIntersection(arr, arr2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
sort
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 break down what is being tested in this benchmark. **Benchmark Purpose** The purpose of this benchmark is to compare the performance of two approaches for finding the intersection of two arrays: using Lodash's `intersection` function and implementing a custom solution. **Options Compared** 1. **Lodash's `intersection` function**: This approach uses the built-in `intersection` function from the Lodash library, which takes two arrays as input and returns an array containing only the elements that are common to both arrays. 2. **Custom implementation**: The custom implementation uses a Set-based approach to find the intersection of two arrays. It creates two Sets from the input arrays, sorts them based on their size, and then iterates over the smaller Set to find the common elements. **Pros and Cons** 1. **Lodash's `intersection` function**: * Pros: Fast, lightweight, and easy to use. * Cons: External dependency, may not be suitable for all environments or browsers. 2. **Custom implementation**: * Pros: No external dependencies, can be optimized for specific use cases. * Cons: More complex code, requires manual Set management. **Library used** The benchmark uses the Lodash library version 4.17.5. **Special JavaScript feature/syntax** There are no special JavaScript features or syntax used in this benchmark. **Other considerations** 1. **Array size**: The benchmark generates two arrays of length 51 using random integers, which may not be representative of real-world scenarios. 2. **Hardware and software variability**: The benchmark's results may vary depending on the hardware and software configuration being tested (e.g., different browsers, operating systems, or devices). **Alternative approaches** 1. **Using other intersection functions**: Other libraries like Underscore.js or a custom implementation using `Array.prototype.filter()` can be used as alternatives. 2. **Optimizing the custom implementation**: Further optimization techniques, such as using more efficient data structures (e.g., hash tables) or parallel processing, could improve the performance of the custom implementation. 3. **Using native JavaScript methods**: Some modern browsers and Node.js versions provide native methods for finding array intersections, such as `Array.prototype.filter()` with a callback function. Overall, this benchmark provides a useful comparison between two approaches to finding the intersection of arrays, highlighting the trade-offs between using an existing library versus implementing a custom solution.
Related benchmarks:
Lodash.filter vs array.filter
Lodash max vs Math.max (lodash 4.7.11)
Array Intersection vs. Set Intersection vs. Lodash - big
Lodash.isArray vs Array.isArray (Lodash v4.17.15)
Comments
Confirm delete:
Do you really want to delete benchmark?