Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comparing performance of: _.uniqWith vs native impl vs _.uniqBy
(version: 0)
Comparing performance of:
imp vs lodash.uniqWith vs lodash.uniqBy
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
function _uniqTxes(newSelectedModelList) { return newSelectedModelList.filter(function({ txid }, key) { return !this.has((key = txid)) && this.add(key); }, new Set()); } // Build random data set with duplicates function randomChar() { return 'abcdefghijklmnopqrstuvwxyz'.charAt(Math.floor(Math.random() * 26)); } var data = []; for (var i = 0; i < 50000; i++) { data.push ({ txid: randomChar() + randomChar() +randomChar() }); }
Tests:
imp
_uniqTxes(data)
lodash.uniqWith
_.uniqWith(data, (a, b) => a.txid === b.txid);
lodash.uniqBy
_.uniqBy(data, 'txid');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
imp
lodash.uniqWith
lodash.uniqBy
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
imp
313.4 Ops/sec
lodash.uniqWith
0.6 Ops/sec
lodash.uniqBy
373.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark compares the performance of three approaches for removing duplicates from an array: a native implementation (`imp`), `_.uniqWith` from Lodash, and `_.uniqBy` from Lodash. The test case uses a random dataset with 50,000 elements to simulate real-world scenarios. **Native Implementation (`imp`)** The native implementation uses the `Array.prototype.filter()` method to remove duplicates. It checks if each element exists in a set (using `has`) and adds new elements to the set as they are encountered. This approach relies on the set data structure to efficiently check for duplicates. Pros: * No external dependencies, making it more platform-agnostic. * Can be optimized further using techniques like caching or memoization. Cons: * May have performance overhead due to the use of a set data structure and function calls. * Less concise code compared to other approaches. **Lodash's `_.uniqWith`** This method uses a provided comparison function to determine uniqueness. In this case, it compares each element based on its `txid` property using the `a.txid === b.txid` condition. Pros: * Provides more flexibility in terms of comparing different properties. * No additional dependencies required beyond Lodash. Cons: * May have performance overhead due to the use of a function call and property access. * Less concise code compared to other approaches. **Lodash's `_.uniqBy`** This method uses a provided key function to determine uniqueness. In this case, it compares each element based on its `txid` property using the `a.txid === b.txid` condition, similar to `_.uniqWith`. Pros: * Provides more flexibility in terms of comparing different properties. * No additional dependencies required beyond Lodash. Cons: * May have performance overhead due to the use of a function call and property access. * Less concise code compared to other approaches. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various helper functions for tasks like array manipulation, string manipulation, and more. `_.uniqWith` and `_.uniqBy` are part of the Lodash library. Pros: * Provides a convenient way to perform common operations like uniqueness checks. * Well-maintained and widely used, ensuring stability and compatibility. Cons: * Adds an external dependency, which may not be desirable in all scenarios. **Other Considerations** When comparing these approaches, consider the trade-offs between performance, code conciseness, and maintainability. For most use cases, Lodash's `_.uniqWith` or `_.uniqBy` might be a better choice due to their flexibility and convenience. However, for specific scenarios where performance is critical, the native implementation (`imp`) might be a better option. **Alternatives** If you prefer not to use Lodash, there are alternative libraries like Underscore.js (which predates Lodash) or other implementation-specific solutions that provide similar functionality. Alternatively, you can implement your own uniqueness checks using the built-in `Array.prototype.filter()` method and set data structure.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?