Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Intersection vs. Set Intersection vs. Lodash vs Sets
(version: 0)
Comparing performance of:
Javascript Set intersection vs Lodash intersection vs Javascript Array intersection vs set2
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:
var first = [...Array(100)].map(it => ~~(Math.random() * 1000)); var second = [...Array(20)].map(it => ~~(Math.random() * 1000));
Tests:
Javascript Set intersection
const firstSet = new Set(first); const secondSet = new Set(second); new Set([...firstSet].filter(item => secondSet.has(item)));
Lodash intersection
_.intersection(first, second)
Javascript Array intersection
first.filter(it => second.includes(it))
set2
const setA = new Set(first); const setB = new Set(second); const _intersection = new Set() for (const elem of setB) { if (setA.has(elem)) { _intersection.add(elem) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Javascript Set intersection
Lodash intersection
Javascript Array intersection
set2
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 provided benchmark. **Benchmark Overview** The benchmark compares four different approaches to find the intersection of two arrays: Lodash, Set (using JavaScript built-in `Set` data structure), and two custom implementations using JavaScript arrays (`filter()` and manual iteration). **Options Compared** 1. **Lodash**: The `intersection()` function from Lodash is used to find the intersection of two arrays. 2. **Set**: A JavaScript built-in `Set` data structure is used, which provides a fast way to perform set operations (union, intersection, difference). 3. **Array `filter()`**: The `filter()` method is used on one of the arrays (`first`) to create a new array containing only the elements that are also present in the other array (`second`). 4. **Manual Iteration**: A custom implementation using manual iteration is used to find the intersection of two sets. **Pros and Cons** 1. **Lodash**: * Pros: Easy to use, well-tested, and widely adopted. * Cons: May not be optimized for performance, adds extra overhead due to function call. 2. **Set**: * Pros: Fast, efficient, and native JavaScript implementation. * Cons: Requires understanding of set operations and data structure. 3. **Array `filter()`**: * Pros: Simple and easy to understand. * Cons: May not be optimized for performance, creates a new array with duplicates (if elements appear in both sets). 4. **Manual Iteration**: * Pros: Customizable, control over iteration logic. * Cons: More complex, error-prone, and may have performance overhead. **Library** The `lodash` library is used as the third-party utility for performing set operations. Lodash provides a simple and efficient way to perform set intersections, unions, and differences. **Special JavaScript Features/Syntax** None of the benchmark test cases explicitly use special JavaScript features or syntax (e.g., async/await, ES6 classes). However, it's worth noting that some browsers may have implemented certain experimental features or optimizations that might affect the results. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Array `reduce()`**: An alternative to `filter()`, but with different performance characteristics. 2. **`includes()` method on arrays**: A simpler way to check if an element is present in an array, but may not be as efficient for large datasets. 3. **Custom implementation using `for...of` loop**: Another approach to finding the intersection of two sets using a custom iteration loop. Keep in mind that each alternative has its own trade-offs and performance characteristics, which might affect the results of your benchmarking tests.
Related benchmarks:
Array Intersection vs. Set Intersection vs. Lodash
Array Intersection vs. Set Intersection vs. Lodash part 3
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?