Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native intersect vs lodash intersection 2
(version: 0)
Comparing performance of:
native vs lodash
Created:
4 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:
function intersect(...array) { return [...new Set(array.reduce((a, b) => a.filter(c => b.includes(c))))]; } var a1 = []; var a2 = []; var a3 = []; for (var i = 0; i < 1000; i++) { a1.push(~~(Math.random() * 100)) a2.push(~~(Math.random() * 100)) a3.push(~~(Math.random() * 100)) }
Tests:
native
intersect(a1, a2, a3);
lodash
_.intersection(a1, a2, a3);
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:
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. The provided JSON represents two test cases: a native JavaScript implementation and a version using the popular library Lodash for array intersection. **Native Implementation** In this test case, we have a simple JavaScript function `intersect` that takes an array as input. The function uses the following approach to perform the intersection: 1. Create a new Set from the input array. 2. Filter the original array by checking if each element is included in the Set created in step 1. The pros of this approach are: * Native JavaScript, which means it's optimized for performance and has no dependencies on external libraries. * Simple implementation, which makes it easy to understand and maintain. However, there are some cons to consider: * The `includes` method used in the filter condition can be slower than other methods, as it needs to traverse the entire Set to check if an element is present. * Creating a new Set from the input array can lead to increased memory usage, especially for large arrays. **Lodash Implementation** In this test case, we have a version of the `intersect` function using Lodash's `_.intersection` method. The pros of this approach are: * Lodash provides an optimized implementation for array intersection, which is likely to be faster than the native JavaScript approach. * The `_.intersection` method takes care of creating and filtering the result set, so we don't need to worry about memory usage or performance. However, there are some cons to consider: * Dependency on Lodash, which adds a small overhead in terms of size and load time. * The implementation might be less straightforward than the native JavaScript version, as it relies on an external library. **Special Consideration** There's no special feature or syntax used in this benchmark, but it's worth noting that some modern browsers like Chrome 91 use WebAssembly (WASM) to execute JavaScript code. If MeasureThat.net were to test WASM execution, the results would likely be different due to the optimized compilation and execution environment. **Other Alternatives** If you're interested in exploring alternative approaches for array intersection, here are a few options: * **Array.prototype.filter()**: You can use `filter()` method on each element of the arrays to perform the intersection. This approach is simpler than creating Sets or using libraries like Lodash. * **Array.prototype.reduce()**: You can use `reduce()` method on each element of the arrays to perform the intersection. This approach is similar to the native implementation but uses a different method. * **External Libraries**: Besides Lodash, there are other libraries available that provide optimized implementations for array intersection, such as `array-intersection` or `fast-intersect`. In summary, the native JavaScript implementation and Lodash implementation represent two different approaches to performing array intersection. While the native approach is simple and optimized for performance, it may come with some overhead in terms of memory usage. The Lodash implementation provides a more efficient solution but relies on an external library, which might be a consideration depending on your project's requirements.
Related benchmarks:
Array Intersection vs. Set Intersection vs. Lodash
native intersect vs lodash intersection
Array Intersection vs. Set Intersection vs. Lodash - big
Array Intersection vs. Set Intersection vs. Lodash part 5
Comments
Confirm delete:
Do you really want to delete benchmark?