Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs es6 (intersection)1
(version: 0)
Comparing performance of:
lodash vs es6 set vs es6 loop
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
Script Preparation code:
var dataset = [...Array(20)].map((v, idx) => idx); var candidates = [1, 2, 5];
Tests:
lodash
var flag = candidates.length === (_.intersection(dataset, candidates)).length
es6 set
var ds = new Set(dataset); var cs = new Set(candidates); var flag = candidates.length === ([...ds].filter(v => cs.has(v))).length;
es6 loop
var flag = true; dataset.forEach((v) => { if (!candidates.includes(v)) { flag = false; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
es6 set
es6 loop
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 the provided benchmarking test. **Overview** The test is designed to compare three approaches for finding the intersection of two arrays: 1. Lodash (specifically, its `intersection` function) 2. ES6 Set (using `Set` objects and the `has` method) 3. A custom loop-based approach **Test Case Analysis** Each test case consists of a benchmark definition, which is a JavaScript expression that calculates the intersection of two arrays. The test case is repeated multiple times to measure performance. 1. **Lodash**: The benchmark definition uses Lodash's `intersection` function, which takes two arrays as input and returns their intersection. ```javascript var flag = candidates.length === (_.intersection(dataset, candidates)).length; ``` The `_` variable likely refers to the global object (in this case, `window`). Pros: Lodash provides a concise and efficient way to calculate intersections. Its implementation is optimized for performance. Cons: Using an external library adds overhead due to loading and initialization times. 2. **ES6 Set**: The benchmark definition uses two `Set` objects to store the input arrays and calculates their intersection using the `has` method. ```javascript var ds = new Set(dataset); var cs = new Set(candidates); var flag = candidates.length === ([...ds].filter(v => cs.has(v))).length; ``` Pros: This approach uses built-in JavaScript features, making it a lightweight solution. It's also easy to understand and implement. Cons: The performance might be slower than the Lodash implementation due to the overhead of creating and filtering sets. 3. **Custom Loop**: The benchmark definition uses a simple loop-based approach to find the intersection. ```javascript dataset.forEach((v) => { if (!candidates.includes(v)) { flag = false; } }); ``` Pros: This approach is easy to understand and implement, as it only requires basic array operations. Cons: The performance might be slower than the other two approaches due to the overhead of looping through arrays. **Library and Feature Considerations** * **Lodash**: Lodash is a popular JavaScript utility library that provides various functions for tasks like string manipulation, number formatting, and data manipulation. In this case, it's used for its `intersection` function. * **ES6 Set**: This feature was introduced in ECMAScript 2015 (ES6) as part of the new standard for working with sets. It provides a lightweight and efficient way to store unique values. **Other Alternatives** If you're looking for alternative approaches, you could consider: * Using `filter` and `reduce` functions on arrays * Employing a more specialized library like `intersection-leave` * Implementing your own custom intersection algorithm using bitwise operations However, these alternatives might not provide the same level of performance or readability as the Lodash implementation. **Benchmark Considerations** When benchmarking, it's essential to consider factors like: * Input size and distribution * JavaScript engine optimizations (e.g., caching, inlining) * Library loading times and initialization overhead * Test case variability (e.g., using different input arrays) By considering these factors, you can ensure that your benchmarks are fair and representative of real-world performance scenarios.
Related benchmarks:
Lodash vs. Native
lodash vs es6 (intersection)
Lodash Intersection vs. ES6 set has
Array Intersection vs. Set Intersection vs. Lodash part 5
Comments
Confirm delete:
Do you really want to delete benchmark?