Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Intersection vs. Set Intersection vs. Lodash
(version: 0)
Comparing performance of:
Javascript Set intersection vs Lodash intersection vs Javascript intersection
Created:
5 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 intersection
first.filter(it => second.includes(it))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Javascript Set intersection
Lodash intersection
Javascript intersection
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Javascript Set intersection
674273.8 Ops/sec
Lodash intersection
626160.1 Ops/sec
Javascript intersection
1236802.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmarking results to understand what's being tested. **Benchmark Overview** The test compares three approaches for finding the intersection of two arrays: 1. **Set Intersection**: Uses `Set` objects in JavaScript, which are implemented as hash tables for efficient lookups. 2. **Lodash Intersection**: Utilizes the `intersection` function from the Lodash library, a popular utility belt for functional programming tasks. 3. **JavaScript Intersection** (also known as **Filter Method**): Employs the `.filter()` method to find common elements between two arrays. **Options Compared** The test compares the performance of these three approaches: * Set Intersection: Creates two `Set` objects from the input arrays and then uses another `Set` constructor with a filter method to compute the intersection. * Lodash Intersection: Uses the `intersection` function from Lodash, which takes two arrays as arguments and returns their intersection. * JavaScript Intersection (Filter Method): Converts one of the input arrays into a `Set`, and then filters it against the other array. **Pros and Cons** Here's a brief analysis of each approach: 1. **Set Intersection** * Pros: + Efficient lookup times due to hash table implementation. + Simple implementation. * Cons: + Requires creating additional `Set` objects, which might incur overhead for small arrays. 2. **Lodash Intersection** * Pros: + Reusable and flexible utility function. + Can be used as a building block for more complex operations. * Cons: + Adds an external dependency (Lodash). + Might have slower performance compared to native JavaScript implementations due to function overhead. 3. **JavaScript Intersection (Filter Method)** * Pros: + Native JavaScript implementation with minimal dependencies. + Can be optimized for specific use cases (e.g., using `Set` objects under the hood). * Cons: + Requires converting one of the input arrays into a `Set`, which can be slower for small inputs. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of functional programming utilities. The `intersection` function is part of this utility belt and allows you to compute the intersection of two arrays efficiently. **Special JS Feature/Syntax** None mentioned explicitly in the provided benchmark code, but it's worth noting that some modern JavaScript features like **Symbol** or **BigInt** might be used for more advanced benchmarking scenarios. However, these are not present in this specific example. **Other Alternatives** For finding array intersections, you can also consider other libraries or implementations, such as: * **Underscore.js**: Another utility belt with an `intersection` function. * **Array.prototype.filter()**: A built-in JavaScript method for filtering arrays. * **Splice()**: An array method that removes elements from the end of an array while preserving the relative order. Keep in mind that the choice of implementation often depends on specific requirements, such as performance, memory usage, or code simplicity.
Related benchmarks:
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?