Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
vanilla vs lodash array difference
(version: 0)
Comparing performance of:
_.difference vs _.filter vs filter (es6)
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
Script Preparation code:
var listA = _.range(10000) var listB = _.range(9000)
Tests:
_.difference
const r1 = _.difference(listA, listB) console.log(1, r1)
_.filter
const r2 = _.filter(listA, x => !_.includes(listB, x)) console.log(2, r2)
filter (es6)
const r3 = listA.filter(x => !listB.includes(x)) console.log(3, r3)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.difference
_.filter
filter (es6)
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 the provided JSON benchmark data. **Benchmark Definition** The benchmark measures the performance of three different approaches to find the elements in an array that are not present in another array: 1. `_.difference(listA, listB)` - This uses the Lodash library (`lodash`) to calculate the difference between two arrays. 2. `_.filter(listA, x => !_.includes(listB, x))` - This uses Lodash to filter elements from one array based on their absence in another array using `includes`. 3. `listA.filter(x => !listB.includes(x))` - This is a vanilla JavaScript implementation of the same logic as the second approach. **Options Compared** The benchmark compares the performance of these three approaches: * **Pros and Cons:** + `_.difference(listA, listB)`: This approach uses Lodash's efficient array operations. However, it requires loading an external library. + `_.filter(listA, x => !_.includes(listB, x))`: This approach uses Lodash's utility functions to filter elements. It also requires loading an external library. + `listA.filter(x => !listB.includes(x))`: This is a vanilla JavaScript implementation that relies on the built-in `includes` method. It does not require any additional libraries, but its performance might be slower due to the slower `includes` method. * **Other Considerations:** + The benchmark assumes that both arrays are present in memory and can be processed without significant overhead. + It also assumes that the `includes` method is relatively slow compared to other array operations. **Library: Lodash** Lodash is a utility library for JavaScript that provides a set of functional programming helpers, including array operations like `difference`, `filter`, and others. The library provides efficient implementations of common tasks, making it easier to write maintainable code. In the benchmark, Lodash is loaded as an external script using the HTML Preparation Code (`<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>`). This allows the test to focus on the performance differences between the three approaches rather than the overhead of loading a library. **Special JS Features or Syntax:** There are no special JavaScript features or syntax used in this benchmark beyond what's required for the array operations themselves (e.g., using `includes` and arrow functions). **Alternatives:** If you wanted to implement a similar benchmark without relying on Lodash, you could consider: * Using native JavaScript methods like `filter()` and `some()`, which would eliminate the need for an external library. * Implementing your own efficient array difference algorithms, such as using bit manipulation or sorting-based approaches. * Utilizing other libraries that provide similar functionality to Lodash, such as Ramda or Sane.
Related benchmarks:
_.range() vs Array.from() vs [...Array().keys()]
lodash range vs Array.from vs keys() + spread
_.range(N) vs [...Array(N).keys()]
lodash range vs Array.from vs keys() + spread 234das
lodash times vs Array.from
Comments
Confirm delete:
Do you really want to delete benchmark?