Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce-vs-uniqBy
(version: 0)
Compare different ways of extracting unique items
Comparing performance of:
Array.prototype.reduce() vs _.uniqBy
Created:
7 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), }, }));
Tests:
Array.prototype.reduce()
workOrders.reduce((accumulator, workOrder) => { if (workOrder.assignee === null) { return accumulator; } const assigneeExists = accumulator.some(assignee => assignee.id === workOrder.assignee.id); return assigneeExists === true ? accumulator : [...accumulator, workOrder.assignee]; }, []);
_.uniqBy
_(workOrders).map('assignee').filter().uniqBy('id').value();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.reduce()
_.uniqBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.reduce()
5162223.0 Ops/sec
_.uniqBy
1617437.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare two approaches for extracting unique items from an array: `Array.prototype.reduce()` and `_uniqBy` (a function from the Lodash library). **Test Cases** There are two test cases: 1. **`Array.prototype.reduce()`**: This test case uses the built-in `reduce()` method of JavaScript arrays to extract unique items. The benchmark measures how long it takes for this method to execute. 2. **`_.uniqBy` (Lodash)**: This test case uses a function from the Lodash library, `_uniqBy`, which is designed to extract unique items based on a specific property. **Options Compared** The two approaches differ in their implementation: * `Array.prototype.reduce()`: + Uses a callback function to iterate over the array. + Accumulates a new array with unique elements by checking if each element's ID exists in the accumulator array. + Returns an empty array if no elements exist, or a new array with the first existing element. * `_uniqBy` (Lodash): + Uses a filter and map approach to extract unique items based on the `id` property. + Iterates over the filtered array and maps each element to its own value, creating a set of unique values. **Pros and Cons** Here are some pros and cons for each approach: * `Array.prototype.reduce()`: + Pros: Lightweight, simple, and efficient for small arrays. + Cons: Can be slow for large arrays due to the callback function and accumulator array creation. * `_uniqBy` (Lodash): + Pros: Fast and efficient for large arrays, as it uses a set data structure under the hood. + Cons: Requires Lodash library inclusion, which may add overhead. **Other Considerations** When choosing between these approaches, consider the size of your array and performance requirements. For small to medium-sized arrays, `Array.prototype.reduce()` might be sufficient. However, for large datasets, `_uniqBy` (Lodash) is likely a better choice due to its efficiency. **Library and Syntax** The Lodash library provides a convenient way to extract unique items from arrays using `_uniqBy`. No special JavaScript features or syntax are required for this test case. **Alternatives** If you don't have access to the Lodash library, you can implement your own `_uniqBy` function using other data structures like sets or maps. Here's an example implementation: ```javascript function uniqBy(arr, key) { const seen = new Map(); return arr.filter(x => { if (seen.has(x[key])) return false; seen.set(x[key], true); return true; }); } ``` This implementation uses a `Map` to keep track of unique values and a filter to remove duplicates. Note that this implementation has a different syntax than `_uniqBy`, but it achieves the same goal.
Related benchmarks:
reduce-vs-uniqBy_2
uniq-vs-uniqBy
reduce-vs-uniqBy 1234
uniq-vs-uniqWith
Comments
Confirm delete:
Do you really want to delete benchmark?