Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash difference vs reduce vs filter vs find
(version: 0)
exclude all the elements for one array from another array
Comparing performance of:
lodash vs reduce vs set filter vs find filter
Created:
4 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 list_1 = [] var list_2 = [] for(var i =0;i<1000;i++){ list_1.push(Math.random().toString(36).slice(2, 7)); } for(var i =0;i<200;i++){ list_2.push(Math.random().toString(36).slice(2, 7)); }
Tests:
lodash
_.difference(list_1, list_2)
reduce
var excludeMap = list_2.reduce((all, item) => ({ ...all, [item]: true }), {}); list_1.filter((item) => !excludeMap?.[item]);
set filter
var toRemove = new Set(list_2); list_1.filter(i => !toRemove.has(i))
find filter
list_1.filter(i => list_2.find(i2 => i2 === i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash
reduce
set filter
find filter
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 break down what's being tested in this benchmark. **Benchmark Overview** The benchmark measures the performance of three different approaches to exclude all elements from one array (`list_1`) based on their presence in another array (`list_2`). **Approaches Compared** 1. **Lodash**: Uses the `_.difference` function from the Lodash library, which returns a new array containing only the elements that are present in `list_1`, but not in `list_2`. 2. **Reduce**: Uses the `reduce` method to create a map of all elements in `list_2` as keys and sets them as true, then uses this map to filter out elements from `list_1` that are not present. 3. **Set Filter**: Uses a Set data structure to store elements from `list_2`, then filters out elements from `list_1` that are not present in the Set. **Pros and Cons of Each Approach** 1. **Lodash**: * Pros: Simple, concise, and well-tested function. * Cons: Requires an external library (Lodash), which might introduce additional overhead. 2. **Reduce**: * Pros: Native JavaScript function, no external dependencies. * Cons: Can be slower due to the creation of a map, and might require more CPU cycles for large arrays. 3. **Set Filter**: * Pros: Fast lookups, especially for large sets. * Cons: Requires creating an additional Set data structure, which can consume memory. **Library Used** The benchmark uses Lodash version 4.17.5, which is a popular and widely-used library for functional programming utilities in JavaScript. **Special JS Feature/Syntax** None of the individual test cases use any special JavaScript features or syntax that would require additional explanation. **Other Alternatives** Other approaches to exclude elements from an array based on their presence in another array could include: * Using `filter()` with a callback function that checks for presence in `list_2`. * Using a native implementation of the difference operation, such as using bitwise operations or sorting and indexing. * Using a more modern JavaScript approach, such as using `Set` and `Map` data structures, but without using Lodash. In general, the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Array.prototype.filter vs Lodash filter
Array.prototype.filter vs Lodash without
Array.prototype.filter vs Lodash.without
Array.prototype.filter vs Lodash filter removing item from array
Array.prototype.filter vs Lodash without 2
Comments
Confirm delete:
Do you really want to delete benchmark?