Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq-vs-uniqBy-bigger-objects
(version: 0)
Compare different ways of extracting unique items
Comparing performance of:
_.uniq() vs _.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.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), } } } }, }));
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):
I'll break down the provided benchmark definition, options being compared, pros and cons of those approaches, and other considerations. **Benchmark Definition** The benchmark compares two ways to extract unique items from an array: `_.uniq()` and `_.uniqBy()`. The latter takes an additional argument `'id'`, which suggests that the uniqueness is based on the values of the 'id' property in each object. **Options Being Compared** 1. **_.uniq()**: This method removes duplicate elements from an array, considering all properties of the objects. It returns a new array with unique elements. 2. **_.uniqBy()**: This method removes duplicate elements from an array based on the values of a specified property (in this case, `'id'`). It returns a new array with unique elements. **Pros and Cons** * **_.uniq()**: + Pros: Simple to implement, easy to understand. + Cons: May be slower for large arrays due to its linear search algorithm. * **_.uniqBy()**: + Pros: More efficient for large arrays since it uses a Map data structure to keep track of unique values, reducing the time complexity to O(n). + Cons: Requires an additional property to be specified (in this case, `'id'`), which may add complexity. **Library and Its Purpose** The Lodash library is used in this benchmark. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and more. The `_.uniq()` and `_.uniqBy()` functions are part of the Lodash library, providing efficient ways to remove duplicate elements from arrays. **Special JS Feature or Syntax** There doesn't appear to be any special JavaScript features or syntax used in this benchmark beyond what is standard for Lodash. **Other Considerations** * The benchmark uses a random array generation function (`_.range(_.random(100)).map(x => ...)` to create an array of work orders) and assigns random IDs, names, and other properties to each order. This ensures that the output is likely to be different on each run. * The benchmark is run in Chrome 112 on a Mac OS X 10.15.7 system, which may affect performance due to browser-specific optimizations. **Alternative Approaches** 1. Using `Array.prototype.filter()` and `Array.prototype.includes()`: This approach would involve iterating over the array using `filter()` to remove duplicates based on a specified property. 2. Using a custom implementation with a data structure like a Set or Map: This approach would involve creating a data structure to keep track of unique elements, which may be more efficient than Lodash's implementation for very large arrays. In general, the choice between `_.uniq()` and `_.uniqBy()` depends on the specific requirements of your use case. If you need to remove duplicates based on any property, `_.uniqBy()` might be a better choice. However, if simplicity is more important than performance, `_.uniq()` could still be sufficient for most use cases.
Related benchmarks:
reduce-vs-uniqBy
uniq-vs-uniqBy
uniq-vs-uniqWith
uniq-vs-uniqBy-even-bigger-objects
Comments
Confirm delete:
Do you really want to delete benchmark?