Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash difference vs ES6 Set (large data set and small)
(version: 0)
Comparing performance of:
_difference small to large vs Set/Filter small to large vs _difference small to small vs Set/Filter small to small
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 < 20000; i++) { arr1.push('' + i); } var arr1a = []; for(let i = 2000; i >= 0; i--) { arr1a.push('' + i); } var arr2 = []; for(let i = 499; i >= 0; i--) { arr2.push('' + i); }
Tests:
_difference small to large
const finalArray = _.difference(arr2, arr1)
Set/Filter small to large
const outList = new Set([...arr1]); const finalArray = arr2.filter(value => !outList.has(value));
_difference small to small
const finalArray = _.difference(arr2, arr1a)
Set/Filter small to small
const outList = new Set([...arr1a]); const finalArray = arr2.filter(value => !outList.has(value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_difference small to large
Set/Filter small to large
_difference small to small
Set/Filter small to small
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
gemma2:9b
, generated one year ago):
This benchmark compares two different approaches for finding the difference between two arrays in JavaScript: **1. Lodash's `_.difference()` method:** This approach utilizes the `_difference` function from the Lodash library. Lodash is a popular library that provides utility functions and methods to simplify common JavaScript tasks. In this case, it's used for efficiently finding elements present in one array but not another. **2. ES6 Set and `filter()` method:** This approach leverages built-in JavaScript features: * **`Set`**: A data structure that stores unique values, allowing for efficient membership checking. * **`filter()`**: A method that creates a new array with elements that pass a specific test. **Let's break down the options:** * **Lodash `_.difference()` (Pros):** * **Concise and Readable:** Often provides a more straightforward syntax compared to manual implementations. * **Potentially Faster:** Lodash is highly optimized, and its `_.difference()` function might be faster than manually implementing the logic using `Set` and `filter`. * **Lodash `_.difference()` (Cons):** * **Dependency:** Requires including the Lodash library in your project, adding an extra dependency. * **ES6 Set and `filter()` (Pros):** * **No External Dependencies:** Uses only built-in JavaScript features, so no need to include additional libraries. * **Modern Syntax:** Embraces modern JavaScript concepts like Sets. * **ES6 Set and `filter()` (Cons):** * **Potentially Less Efficient:** While `Set` is efficient for membership checking, the combination with `filter` might not be as optimized as Lodash's specialized function in certain scenarios. **Other Considerations:** * **Data Size:** For very large arrays, the performance difference between these approaches might become more significant. * **Specific Use Case:** If you need other functionality provided by Lodash, using `_.difference()` might be advantageous even if the `Set/filter` approach is slightly faster for this specific task. **Alternatives:** * **Manual Implementation:** You could implement your own difference-finding algorithm using loops and comparisons. This gives you full control but can be less efficient than optimized libraries or built-in methods. * **Specialized Libraries:** There might be other specialized JavaScript libraries focused on array manipulation that offer even more efficient or tailored solutions for finding differences. **Remember:** The "best" approach often depends on the specific context of your project, including factors like performance requirements, code maintainability, and existing dependencies.
Related benchmarks:
Lodash difference vs Set & Filter vs Map
lodash difference vs ES6 Set (large set)
lodash difference vs ES6 Set (large data set)
lodash difference vs ES6 Set (small data set)
Array.slice() vs. Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?