Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
lodash UniqueWith vs filter
(version: 0)
Benchmark.js
Comparing performance of:
_.uniqWith vs filter
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var myArray = Array(3000).fill({a:1, b:2});
Tests:
_.uniqWith
myArray = _.uniqWith(array, _.isEqual);
filter
const trackObj = {}; myArray = myArray.filter(item => { if (trackObj[item.a] && trackObj[item.a] === item.b) { return false; } trackObj[item.a] = item.b; return true; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniqWith
filter
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; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
filter
22.4M/s
_.uniqWith
0/s
View exact numbers
Test name
Executions per second
✓
filter
22,378,852 Ops/sec
_.uniqWith
0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the provided benchmark. **Benchmark Overview** The benchmark is comparing two approaches for removing duplicates from an array: `_.uniqWith` from Lodash and the built-in `filter()` method. **Lodash `.uniqWith`** `_.uniqWith` is a function from the Lodash library that removes duplicate elements from an array based on a custom comparison function. In this benchmark, the comparison function used is `_isEqual`, which checks for deep equality between two objects. Pros: * Efficient: `_.uniqWith` uses a clever algorithm to remove duplicates in O(n) time complexity. * Customizable: The comparison function allows users to tailor the behavior to their specific use case. Cons: * Additional dependency: Using Lodash introduces an extra dependency, which may not be desirable for small projects or those with strict dependencies constraints. * Performance overhead: Some browsers might incur a slight performance overhead due to the additional JavaScript execution. **Built-in `filter()` method** The built-in `filter()` method is a vanilla JavaScript function that creates a new array with only the elements that pass a test implemented by a provided function. Pros: * Native support: The `filter()` method is native to JavaScript, so it doesn't require any additional dependencies or libraries. * Lightweight: The `filter()` method is generally faster and more lightweight than using Lodash. Cons: * Less flexible: The `filter()` method only provides basic filtering functionality; it's not as powerful as `.uniqWith`. **Other considerations** In terms of special JavaScript features, this benchmark doesn't use any advanced techniques like async/await, Promises, or modern array methods (e.g., `some()`, `every()`). If you're interested in exploring those topics, there are plenty of other benchmarks that focus on performance optimization. **Alternatives** If you're looking for alternative approaches to remove duplicates from an array, consider the following options: 1. Using a simple array method: You can use methods like `reduce()` or `map()` to create a new array with unique elements. 2. Implementing a custom sorting algorithm: Sorting the array and then removing duplicates using the `filter()` method can be an efficient alternative. 3. Using a data structure like a Set: If you're working in a modern browser that supports Sets, you can convert the array to a Set and then back to an array to remove duplicates. Keep in mind that each approach has its trade-offs, and the best solution depends on your specific use case, performance requirements, and personal preference.
Related benchmarks
uniqBy vs stringify performance
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?