Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Intersection vs. Set Intersection vs. Lodash part 3 mix
(version: 0)
Comparing performance of:
Javascript Set intersection vs Lodash intersection vs Javascript Array intersection vs Js array Set intersection
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))
Js array Set intersection
const secondSetSet = new Set(second); new Set(first.filter( item => secondSetSet.has(item)));
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
Js array Set intersection
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 explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance of three different approaches to find the intersection between two arrays: 1. JavaScript Set Intersection 2. Lodash Intersection 3. JavaScript Array Intersection **What are the options being compared?** The three options being compared are: * JavaScript Set Intersection: This approach uses a `Set` data structure to store the elements of the first array, and then filters out the elements that are not present in the second array. * Lodash Intersection: This approach uses the `intersection()` function from the Lodash library to find the intersection between two arrays. * JavaScript Array Intersection: This approach uses the `filter()` method to create a new array that contains only the elements that are common to both arrays. **Pros and Cons of each approach** Here's a brief summary of the pros and cons of each approach: * **JavaScript Set Intersection**: Pros: + Efficient in terms of time complexity (O(n)) since sets use hash tables for fast lookups. + Can be more memory-efficient than other approaches since it only stores unique elements. * Cons: + Requires creating a `Set` object, which can have additional overhead due to memory allocation and garbage collection. * **Lodash Intersection**: Pros: + Simplifies the code and reduces boilerplate by using a reusable function from the Lodash library. + Can be faster since it's implemented in C++ (compiled JavaScript) and optimized for performance. * Cons: + Requires including an additional library, which may introduce overhead due to loading times and potential conflicts with other libraries. * **JavaScript Array Intersection**: Pros: + Easy to understand and implement, as it uses a familiar `filter()` method. + Does not require creating any additional objects or data structures. * Cons: + Has a higher time complexity (O(n^2)) since the `filter()` method iterates over the elements of one array for each element in the other array. **Library used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array and object manipulation, and more. In this benchmark, the `intersection()` function from Lodash is used to find the intersection between two arrays. **Special JS feature or syntax: Sets (JavaScript)** Sets are a data structure in JavaScript that allows you to store unique values without duplicates. They're implemented as hash tables, which enables fast lookup and insertion operations. In this benchmark, sets are used to implement the JavaScript Set Intersection approach. Overall, the choice of approach depends on the specific requirements of your use case. If performance is critical, JavaScript Set Intersection might be a good choice due to its efficient time complexity. However, if simplicity and ease of implementation are more important, JavaScript Array Intersection might be a better fit.
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
Comments
Confirm delete:
Do you really want to delete benchmark?