Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash difference vs Set & Filter (large)
(version: 0)
Comparing performance of:
Lodash vs Set & Filter vs Set & Filter (new set)
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 arr1 = []; for(let i = 0; i < 10000; i++) { arr1.push('' + i); } var arr2 = []; for(let i = 490; i >= 0; i--) { arr2.push('' + i); }
Tests:
Lodash
const notInArr1 = _.difference(arr2, arr1) const notInArr2 = _.difference(arr1, arr2)
Set & Filter
const arr1Set = new Set(); const arr2Set = new Set(); arr1.forEach(value => arr1Set.add(value)); arr2.forEach(value => arr2Set.add(value)); const notInArr1 = arr2.filter(value => !arr1Set.has(value)); const notInArr2 = arr1.filter(value => !arr2Set.has(value));
Set & Filter (new set)
const arr1Set = new Set([...arr1]); const arr2Set = new Set([...arr2]); const notInArr1 = arr2.filter(value => !arr1Set.has(value)); const notInArr2 = arr1.filter(value => !arr2Set.has(value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
Set & Filter
Set & Filter (new set)
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):
I'll break down the benchmark and its test cases to explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to compare three approaches for finding elements in two large arrays: 1. Using Lodash (`_difference` method) 2. Using a `Set` and filtering (`filter` method) 3. Using a `Set` and filtering with new sets (creating new sets from the original arrays) **Test Cases** Each test case measures the execution time of one of these approaches. ### 1. Lodash The benchmark uses Lodash's `_difference` method to find elements in `arr2` that are not in `arr1`. This method returns a new array containing only the unique elements from the first array and rejecting duplicates. **Pros:** * Efficient for finding differences between two arrays, especially when one of them is large. * Leveraging Lodash's optimized implementation and caching to improve performance. **Cons:** * Adds extra memory usage due to creating a new array. * May incur additional overhead if `arr1` or `arr2` are very large. ### 2. Set & Filter This approach uses two separate `Set` objects to store unique elements from both arrays and then filters out the common elements using the `filter` method. **Pros:** * Does not create new arrays, reducing memory usage. * Can be more efficient for finding intersections between sets. **Cons:** * May incur additional overhead due to set creation and filtering. * Could potentially lead to slower performance if the filter operation is expensive. ### 3. Set & Filter (new set) This approach creates new `Set` objects from both arrays using array spread (`[...arr]`) and then filters out common elements. **Pros:** * Similar to the previous approach, but with a potential performance improvement due to optimized array creation. * Does not create additional memory for filtering results. **Cons:** * Requires creating two new sets, which can lead to increased memory usage. * Still incurs overhead from set creation and filtering. **Library and Syntax Considerations** * Lodash is a popular utility library that provides a wide range of functions for tasks like array manipulation, math operations, and more. In this benchmark, it's used for the `_difference` method. * `Set` objects are a built-in JavaScript data structure that allows you to store unique values. **Device-Specific Considerations** The test results were obtained on a Chrome 108 browser running on a Mac with macOS 10.15.7. The device platform and operating system may affect the benchmark's performance, as some features or optimizations might be unavailable or less efficient on certain platforms. **Other Alternatives** If you need to compare other approaches for finding elements in two arrays, consider using: * `Array.prototype.some()` and `Array.prototype.find()`: These methods can be used to find common or unique elements between two arrays. * `Promise.all()`: This method can be used to compare the performance of multiple array operations in parallel. When choosing an approach, consider factors like memory usage, filter complexity, and potential overhead from set creation.
Related benchmarks:
Lodash difference vs filtering via set membership
Lodash difference vs filtering via set membership with high overlap
Lodash difference vs Set & Filter vs Map
Lodash.filter vs Lodash.without
Array.slice() vs. Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?