Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash difference vs Set & Filter
(version: 3)
Comparing performance of:
Lodash vs Set & Filter
Created:
6 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.5/lodash.min.js'></script>
Script Preparation code:
var arr1 = []; for(let i = 0; i < 100; i++) { arr1.push('' + i); } var arr2 = []; for(let i = 49; i >= 0; i--) { arr2.push('' + i); }
Tests:
Lodash
const notInArr1 = _.difference(arr2, arr1) const notInArr2 = _.difference(arr1, arr2)
Set & Filter
const arr1Set = new Set(); const arr2Set = new Set(); arr1.forEach(value => arr1Set.add(value)); arr2.forEach(value => arr2Set.add(value)); const notInArr1 = arr2.filter(value => !arr1Set.has(value)); const notInArr2 = arr1.filter(value => !arr2Set.has(value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Set & Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
73763.7 Ops/sec
Set & Filter
252676.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case, where two approaches are compared: using Lodash's `difference` method and using Sets with filtering. **Benchmark Definition** The benchmark definition consists of two scripts: 1. **Script Preparation Code**: This code creates two arrays, `arr1` and `arr2`, containing 100 and 51 elements respectively, in ascending order. 2. **Html Preparation Code**: This code includes a reference to the Lodash library (version 4.17.5) from a CDN. **Individual Test Cases** There are two test cases: ### 1. Lodash The benchmark definition for this test case is: ```javascript const notInArr1 = _.difference(arr2, arr1) const notInArr2 = _.difference(arr1, arr2) ``` This code uses the `_.difference` method from Lodash to find the elements that are in `arr2` but not in `arr1`, and vice versa. **Pros and Cons** * **Pros**: Using a well-maintained and widely-used library like Lodash can provide efficient and reliable results. * **Cons**: The use of an external library may introduce additional overhead, such as memory allocation and garbage collection. ### 2. Set & Filter The benchmark definition for this test case is: ```javascript const arr1Set = new Set() const arr2Set = new Set() arr1.forEach(value => arr1Set.add(value)) arr2.forEach(value => arr2Set.add(value)) const notInArr1 = arr2.filter(value => !arr1Set.has(value)) const notInArr2 = arr1.filter(value => !arr2Set.has(value)) ``` This code creates two Sets, `arr1Set` and `arr2Set`, from the arrays `arr1` and `arr2`. It then uses the `filter` method to find the elements that are in one set but not the other. **Pros and Cons** * **Pros**: Using native JavaScript methods (Sets and filtering) can provide a lightweight and efficient solution. * **Cons**: This approach may be slower than using Lodash's optimized implementation, especially for large datasets. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object manipulation, and string manipulation. The `difference` method is part of the Lodash library's set operations capabilities. **Special JS Feature or Syntax** None mentioned in this benchmark. **Other Alternatives** Other alternatives to compare these two approaches could include: * Using native JavaScript methods, such as `Set` and `filter`, without relying on a library like Lodash. * Using a different library, such as `Array.prototype.findIndex()` or `Array.prototype.indexOf()`, for set operations. * Implementing the difference logic from scratch using only native JavaScript features. Keep in mind that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
Lodash difference vs filtering via set membership
Lodash difference vs filtering via set membership with high overlap
Lodash difference vs Set & Filter vs Map
Lodash.filter vs Lodash.without
Lodash filter VS native filter (with Lodash actually loaded)
Comments
Confirm delete:
Do you really want to delete benchmark?