Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq-vs-uniqBy-even-bigger-objects
(version: 0)
Compare different ways of extracting unique items
Comparing performance of:
_.uniq() vs _.uniqBy
Created:
2 years ago
by:
Guest
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 workOrders = _.range(_.random(100)).map(x => ({ assignee: { id: _.random(1, 10), foo: _.random(1, 10), bar: _.random(1, 10), baz: { foo: { bar: _.random(1, 10), baz: { foo: _.random(1, 10), }, }, baz: { foo: { bar: _.random(1, 10), baz: { foo: _.random(1, 10), }, }, baz: { foo: { bar: _.random(1, 10), baz: { foo: _.random(1, 10), }, }, baz: { foo: { bar: _.random(1, 10), baz: { foo: _.random(1, 10), }, }, }, }, }, }, }, }));
Tests:
_.uniq()
_.uniq(workOrders);
_.uniqBy
_.uniqBy(workOrders, 'id');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniq()
_.uniqBy
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 dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark compares two ways of extracting unique items from an array: using Lodash's `uniq` function and `uniqBy` function with a custom key. **_.uniq()** `.uniq()` removes duplicate elements from an array, maintaining their original order. It uses a simple algorithm that iterates over the array, keeping track of seen elements in a data structure called a "hash table" (in JavaScript, this is implemented using an object). When it encounters a new element, it checks if the hash table contains a match; if not, it adds the element to the hash table and returns the unique array. **_.uniqBy()** `.uniqBy()` removes duplicate elements from an array, grouping them by a specified key. It uses a similar algorithm as `.uniq()`, but instead of comparing elements directly, it groups them by the provided key using a data structure called a "map" (in JavaScript, this is implemented using an object). When it encounters a new element with a unique key, it adds it to the map and returns the unique array. **Comparison** The benchmark compares `.uniq()` and `.uniqBy()` on the same input array `workOrders`, which contains objects with multiple properties. The results show that: * `.uniqBy()` is faster than `.uniq()`, executing approximately 1.45 times more executions per second. * Both functions have similar performance characteristics, suggesting that the overhead of using a custom key in `.uniqBy()` does not significantly impact performance. **Pros and Cons** **_.uniq():** Pros: * Simple and efficient algorithm * Fast execution time Cons: * No grouping or filtering capabilities * No support for complex data structures (e.g., nested objects) **_.uniqBy():** Pros: * Allows grouping and filtering by a custom key * Support for complex data structures (e.g., nested objects) * Can be more readable and maintainable than using `.uniq()` with multiple conditions Cons: * Slightly slower execution time compared to `.uniq()` * Additional overhead from using the custom key **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string formatting, and more. In this benchmark, Lodash's `uniq` and `uniqBy` functions are used to demonstrate their performance characteristics. **Special JS Feature/Syntax: None mentioned** This benchmark does not use any special JavaScript features or syntax beyond standard ECMAScript 2020 compliance. **Alternative Implementations** If you want to implement your own version of `.uniq()` or `.uniqBy()`, here are some alternatives: * For `.uniq()`: Use a simple algorithm that iterates over the array and uses an object to keep track of seen elements. * For `.uniqBy()`: Use a similar algorithm as above, but use a map to group elements by the provided key. Keep in mind that implementing your own version may not be as efficient or performant as using a well-optimized library like Lodash.
Related benchmarks:
const vs let vs var
const vs let vs var - proper
var vs let vs const
const vs let vs var fork
const vs let vs var comparison
Comments
Confirm delete:
Do you really want to delete benchmark?