Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native reduce vs uniqBy
(version: 0)
Comparing performance of:
native reduce vs uniqBy
Created:
4 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 data = [ { value: 1, metrics: { metric1: 30, metric2: 0, metric3: 0 } }, { value: 1, metrics: { metric1: 30, metric2: 0, metric3: 0 } }, { value: 1, metrics: { metric1: 30, metric2: 0, metric3: 0 } }, { value: 1, metrics: { metric1: 30, metric2: 30, metric3: 0 } }, { value: 1, metrics: { metric1: 30, metric2: 0, metric3: 30 } }, { value: 1, metrics: { metric1: 30, metric2: 30, metric3: 0 } }, { value: 1, metrics: { metric1: 30, metric2: 0, metric3: 30 } } ]
Tests:
native reduce
Object.values(data.reduce((acc, item) => { const key = JSON.stringify(item.metrics); if (acc[key]) return acc; return { ...acc, [key]: item }; }, {}))
uniqBy
_.uniqBy(data, item => JSON.stringify(item.metrics))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native reduce
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 break down the provided benchmark and explain what is being tested, compared options, pros/cons of those approaches, and other considerations. **Benchmark Definition** The benchmark measures the performance difference between two approaches: using `reduce()` with object values and using `uniqBy()` from the Lodash library to remove duplicate objects based on their `metrics` property. **Options Compared** 1. **Native Reduce**: The first option uses `reduce()` with object values to create a new array of unique objects, where each object is created by concatenating the original item's metrics into its key. 2. **Lodash uniqBy**: The second option uses Lodash's `uniqBy()` function, which takes an array and a callback function that returns a unique key for each item. **Pros/Cons of Approaches** 1. **Native Reduce**: * Pros: Lightweight, no additional library dependency required. * Cons: Can be slower than the Lodash approach due to the overhead of creating object keys from stringified metrics. 2. **Lodash uniqBy**: * Pros: Optimized for performance by using a pre-calculated index on the `metrics` property, reducing the number of iterations. * Cons: Requires an additional library dependency (Lodash). **Library Used** In this benchmark, Lodash is used in two ways: 1. **uniqBy()**: A function that takes an array and a callback function that returns a unique key for each item. It uses its own internal implementation to optimize performance. 2. **lodash.min.js**: The library itself is loaded via a script tag to provide access to the `uniqBy()` function. **Other Considerations** * The benchmark uses a small dataset (4 objects) to demonstrate the performance difference between the two approaches. * The `RawUAString` field in the latest benchmark result indicates that both tests were executed by the same user agent on the same device and platform, ensuring consistent results. * To further improve this benchmark, more extensive testing could be done with larger datasets or different types of data. **Alternatives** Other alternatives for achieving similar functionality include: 1. **Using a custom implementation**: Developing your own optimized function to remove duplicates based on `metrics` property. 2. **Other library functions**: Depending on the specific use case and requirements, other libraries like Ramda, Underscore.js, or even vanilla JavaScript methods (e.g., using `Set`) might be suitable alternatives. Keep in mind that these alternatives would require additional development effort and might not offer the same level of performance as Lodash's optimized implementation.
Related benchmarks:
uniqBy performance
Lodash uniqBy vs Reduce uniqBy
uniqBy performance ttt
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?