Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.js vs Native Intersection1
(version: 0)
Comparing performance of:
Native vs Lodash.js filter
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var max1 = 100000; // 100,000 (100 Thousand) var max11 = 100000; // 100,000 (100 Thousand) var max2 = 10000000; // 10,000,000 (10 Million) var max3 = 100000000; // 100,000,000 (100 Million) var arr1 = []; for (var i = 0; i <= max1; i++) { arr1.push(i); } var arr2 = []; for (var i = 0; i <= max11; i++) { arr2.push(i); } var arr3 = []; //for (var i = 0; i <= max3; i++) { arr3.push(i); }
Tests:
Native
const intersection = (a1 = [], a2 = []) => { const s = new Set(a1) const result = [] for (const x of a2) if (s.has(x)) result.push(x) return result } let c = intersection(arr1, arr2)
Lodash.js filter
let c = _.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.js filter
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):
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The website `MeasureThat.net` allows users to create JavaScript microbenchmarks. The provided benchmark is comparing two approaches for performing an intersection operation on two arrays: a native JavaScript implementation and a Lodash.js implementation. **Native Implementation** The native implementation uses a simple `Set` data structure to keep track of unique elements in the first array (`a1`). It then iterates through the second array (`a2`) and checks if each element is present in the set. If it is, the element is added to the result array. **Lodash.js Implementation** The Lodash.js implementation uses a library function called `_.intersection()` from Lodash, which is an external JavaScript library. This function takes two arrays as input and returns a new array containing only the elements that are present in both arrays. **Comparison** The benchmark compares the performance of these two approaches: 1. Native Implementation (using a Set data structure) 2. Lodash.js Implementation (`_.intersection()`) **Pros/Cons** * **Native Implementation** + Pros: - Fast and lightweight, as it only uses built-in JavaScript data structures. - Easy to understand and implement for developers familiar with JavaScript. + Cons: - May be slower than the Lodash.js implementation due to the overhead of creating and manipulating a Set data structure. * **Lodash.js Implementation** + Pros: - Often faster than native implementations, as it leverages optimized C++ code within the Lodash library. - Convenient for developers who are already familiar with Lodash. + Cons: - Adds external dependency on the Lodash library, which may increase bundle size and complexity. - May require additional setup or installation. **Other Considerations** * The benchmark only tests the intersection operation on arrays. Other operations, such as union, difference, or sorting, may have different performance characteristics. * The use of `const` and `let` keywords in the benchmark definition does not affect the performance comparison. **Alternatives** If you're looking for alternative implementations, you can consider: 1. **Array.prototype.filter()**: Some browsers have optimized `filter()` methods that may outperform the Lodash.js implementation. 2. **Underscore.js**: Another popular JavaScript library that provides a similar `_.intersection()` function to Lodash.js. 3. **Custom implementation using a Map data structure**: If you're comfortable with implementing your own algorithms, a custom solution using a Map data structure might be faster than the native implementation. In summary, the benchmark compares two approaches for performing an intersection operation on arrays: a native JavaScript implementation and a Lodash.js implementation. The native implementation is fast and lightweight but may be slower due to Set overhead, while the Lodash.js implementation is often faster but adds external dependency on the library.
Related benchmarks:
Lodash.js vs Native isArrary
Lodash.js vs Native _.min
Lodash.js vs Native forked
Lodash.js vs Native Intersection
Comments
Confirm delete:
Do you really want to delete benchmark?