Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash: differenceBy vs filter & includes
(version: 0)
Comparing performance of: lodash differenceBy with javascript filter and find
Comparing performance of:
Lodash differenceBy vs javascript filter and find
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var fullArray = [ { title: 'full arr 1', value: '1', }, { title: 'full arr 2', value: '2', }, { title: 'full arr 3', value: '3', } ] var partialArray = [ { id: 1, }, { id: 3, } ]
Tests:
Lodash differenceBy
_.differenceBy( fullArray, partialArray.map(v => ({ value: String(v.id) })), 'value', )
javascript filter and find
var values = partialArray.map(par => String(par.id)) fullArray.filter(full => !values.includes(full.value))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash differenceBy
javascript filter and find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash differenceBy
8826117.0 Ops/sec
javascript filter and find
26897688.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmarking test created using MeasureThat.net. The test compares the performance of two approaches to filter and find elements in an array: Lodash's `differenceBy` method versus a JavaScript implementation using `filter()` and `includes()`. Both approaches aim to find all elements in the `fullArray` that do not match any element in the `partialArray`. **Options Compared** The test compares two options: 1. **Lodash `differenceBy`**: This is a library function from Lodash, a popular JavaScript utility library. It takes three arguments: the array to filter (`fullArray`), the array of objects to compare with (`partialArray.map(v => ({ value: String(v.id) }))`), and a key to determine uniqueness (`'value'`). The function returns a new array containing elements from `fullArray` that do not match any element in `partialArray`. 2. **JavaScript `filter()` and `includes()`**: This is a native JavaScript implementation using the `filter()` method to create a new array with elements that pass a test (in this case, excluding elements that are included in `values`). The `includes()` method checks if an element exists in an array. **Pros and Cons** ### Lodash `differenceBy` Pros: * Often faster than native JavaScript implementations due to its optimized C++ implementation * Simplifies code and reduces boilerplate Cons: * Adds an external dependency (Lodash) * May have higher memory usage due to the additional library ### JavaScript `filter()` and `includes()` Pros: * No external dependencies, reducing overhead * Native performance optimizations can lead to faster execution Cons: * Requires more boilerplate code for complex filtering logic * Can be slower than Lodash's implementation for large arrays **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common programming tasks, such as array manipulation, object merging, and functional programming. The `differenceBy` method is just one example of the many useful utilities available in Lodash. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. Both implementations rely on standard JavaScript methods and functions. **Other Alternatives** If you're interested in exploring alternative libraries for array filtering, some popular options include: * **Mocha**: A testing framework that also provides a `filter()` method * **Underscore.js**: Another utility library with a `differenceBy()` method (note that it's not Lodash) * **Ramda**: A functional programming library with a variety of filtering functions For native JavaScript implementations, you can consider using: * **Array.prototype.filter()** * **Array.prototype.includes()** * **Set** data structures for efficient lookups
Related benchmarks:
Array.prototype.filter vs Lodash filter
Lodash difference vs JS filter and includes
Array.prototype.filter vs Lodash 4.17.5 filter
Array.prototype.filter vs Lodash without 2
Lodash.filter vs Lodash.without
Comments
Confirm delete:
Do you really want to delete benchmark?