Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash difference vs Set & Filter vs Map
(version: 0)
Comparing performance of:
Lodash vs Set & Filter vs map
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 < 100000; i++) { arr1.push({"index": i}); } var arr2 = []; for(let i = 49000; i >= 0; i--) { arr2.push(arr1[i*2]); }
Tests:
Lodash
const notInArr = _.difference(arr2, arr1)
Set & Filter
const arr2Set = new Set(arr2); const notInArr2 = arr1.filter(value => !arr2Set.has(value));
map
const arr2Map = new Map(arr2.map((e2) => ([e2.index, e2]))); const notInArr2 = arr1.filter(value => !arr2Map.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
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
147.5 Ops/sec
Set & Filter
222.3 Ops/sec
map
153.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript benchmark that compares the performance of three different approaches to find elements not present in an array: 1. **Lodash**: Uses the `_difference` function from the Lodash library, which takes two arrays as input and returns a new array containing only the elements present in the first array but not in the second. 2. **Set & Filter**: Creates a `Set` object from the second array (`arr2`) and then uses the `filter` method to find elements in the first array (`arr1`) that are not present in the set. 3. **Map**: Creates a `Map` object from the second array (`arr2`) by mapping each element to its index, and then uses the `has` method to check if an element is present in the map. **Options Compared** The benchmark compares the performance of these three approaches: * Lodash: `_difference` * Set & Filter: using a `Set` object with `filter` * Map: using a `Map` object with `has` **Pros and Cons of Each Approach** 1. **Lodash**: Pros: * Convenient and concise syntax * Well-tested and optimized by the Lodash team * Supports various iteration modes (e.g., `_differenceBy`) Cons: * Requires an additional library download (~2MB) * May incur overhead due to lazy evaluation * Limited control over implementation details 2. **Set & Filter**: Pros: * Lightweight and easy to understand * No additional libraries needed * Fast lookups using `Set` operations Cons: * Requires two separate operations (creating the set and filtering) * May incur overhead due to array resizing for the set 3. **Map**: Pros: * Fast lookups using `has` method * Can be used with existing data structures (e.g., objects, arrays) * Low overhead compared to Set & Filter Cons: * More complex syntax and implementation details * May require additional memory for the map **Library and Purpose** The Lodash library provides a wide range of functional programming utilities, including `_difference`, which is used in this benchmark. The `Set` object is a built-in JavaScript data structure that allows fast membership testing. **Special JS Feature or Syntax** There are no special features or syntaxes mentioned in the provided code. However, note that Lodash uses lazy evaluation, which can impact performance in some cases. **Alternatives** Other alternatives to these approaches include: 1. **Using `find` and `includes`**: Instead of creating a set or map, you could use `find` and `includes` methods to find the first element not present in the array. 2. **Implementing custom iteration**: You could write your own iterative solution using loops instead of relying on built-in functions like Set & Filter. 3. **Using other libraries or frameworks**: Depending on the specific requirements, you might consider using other libraries or frameworks that provide optimized implementations for set and map operations. Overall, the choice of approach depends on the trade-offs between performance, readability, and maintainability.
Related benchmarks:
Lodash map & filter vs reduce with push
Lodash map & filter vs reduce with push and desctructuring (10 000 samples )
Native map & filter vs reduce with push
Lodash filter VS native filter (with Lodash actually loaded)
Comments
Confirm delete:
Do you really want to delete benchmark?