Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs. Set Intersection test1234
(version: 1)
Comparing performance of:
Javascript Set intersection vs Lodash intersection
Created:
one year 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:
const generateRandomArray = (size = 100000, min = 1, max = 1000000) => Array.from({ length: size }, () => Math.floor(Math.random() * (max - min + 1)) + min ); var first = generateRandomArray(); var second = generateRandomArray();
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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Javascript Set intersection
Lodash intersection
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 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
93.0 Ops/sec
Lodash intersection
36.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON compares two methods for finding the intersection of two arrays: using the native JavaScript `Set` object and using the Lodash library's `_.intersection` function. ### Options Compared 1. **JavaScript Set Intersection:** - **Implementation:** ```javascript const firstSet = new Set(first); const secondSet = new Set(second); new Set([...firstSet].filter(item => secondSet.has(item))); ``` - **Description:** This method first converts both arrays (`first` and `second`) into `Set` objects, which automatically eliminate duplicate values. It then filters the elements of the first set based on whether they exist in the second set, resulting in a new `Set` containing only the intersection. 2. **Lodash Intersection:** - **Implementation:** ```javascript _.intersection(first, second); ``` - **Description:** This method leverages the Lodash library's `intersection` function, which takes care of finding common elements in the two provided arrays. It handles duplicates and returns a new array containing the intersection. ### Pros and Cons #### JavaScript Set Intersection - **Pros:** - The use of `Set` is efficient for checking membership (`has` method) and eliminates duplicates inherently, which can be advantageous in many scenarios. - Native to JavaScript; no external library needed, which benefits projects in terms of reduced dependencies and size. - **Cons:** - The performance may vary based on the implementation of `Set` in different JavaScript engines, and the method may not be as straightforward as using a dedicated library for complex set operations. - In older browsers that do not support ES6, `Set` is not available. #### Lodash Intersection - **Pros:** - `_.intersection` is very straightforward; it abstracts the complexity and is easy to use, which can lead to clearer code at a glance. - Lodash is widely used and well-optimized, often performing well across different environments. - **Cons:** - It adds a dependency to the project, which could increase the bundle size if not handled properly. - Performance-wise, it may be slower than a native solution due to additional overhead from the Lodash library's function calls. ### Other Considerations - **Performance Results:** The benchmark results indicate that the JavaScript Set intersection method executes significantly faster (approximately 93 executions per second) compared to the Lodash intersection (approximately 36 executions per second). This finding may suggest that for simpler operations, native JavaScript solutions can outperform library-based approaches in terms of speed. - **Alternatives:** Other methods for array intersection include: - Using `filter` and `includes`: ```javascript const intersection = first.filter(item => second.includes(item)); ``` - Using array comprehension (if supported), or leveraging other utility libraries such as Ramda or Underscore.js, each with their own implementation and potential performance considerations. Overall, the choice between using the native JavaScript Set or Lodash for intersections may depend on project requirements, existing dependencies, compatibility needs across different environments, and performance considerations, particularly for larger datasets.
Related benchmarks:
Lodash vs. Native (find)
Lodash vs. Native (remove/filter)
test 319823789172
Lodash vs. Native (filter)
Array Intersection vs. Set Intersection vs. Lodash vs Sets
Array Intersection vs. natife JS Set Intersection vs. Lodash part 3
Array Intersection vs. Set Intersection vs. Lodash [toby]
dose Array Intersection vs. Set Intersection vs. Lodash part 3
Lodash vs. Set Intersection test123
Comments
Confirm delete:
Do you really want to delete benchmark?