Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce-vs-uniqBy_2
(version: 0)
Compare different ways of extracting unique items
Comparing performance of:
Array.prototype.reduce() vs _.uniqBy
Created:
6 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(1000)).map(x => ({ assigneeId: Math.random().toString(36).slice(3), assigneName: 'LordWarlock' }));
Tests:
Array.prototype.reduce()
workOrders.reduce((accumulator, workOrder) => { if (!workOrder.assigneeId) { return accumulator; } const assigneeExists = accumulator.find(assignee => assignee.assigneeId === workOrder.assigneeId); if (!assigneeExists) { accumulator.push(workOrder) } return accumulator; }, []);
_.uniqBy
_.uniqBy(workOrders, 'assigneeId');
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:
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 and explore what's being tested in this specific benchmark. **Overview** The benchmark compares two approaches to extract unique items from an array: using `Array.prototype.reduce()` versus using the `_` library's `uniqBy()` function. The goal is to determine which approach is faster. **Options compared** There are only two options being compared: 1. **`Array.prototype.reduce()`**: This method applies a callback function to each element in the array, accumulating a value that is returned as the final result. 2. **`.uniqBy()` from the `_` library**: This function takes an array and a property path (in this case, `'assigneeId'`) and returns a new array with unique values. **Pros and Cons** * **`Array.prototype.reduce()`**: + Pros: widely supported, no dependencies. + Cons: can be slower due to the overhead of callback functions, potentially less efficient for large datasets. * **`.uniqBy()` from the `_` library**: + Pros: optimized for performance, handles edge cases like null or undefined values, and is often faster than `Array.prototype.reduce()`. + Cons: requires additional dependencies (the `_` library), which may impact performance. **Library: _** The `_` library is a popular utility library that provides a wide range of functions for common tasks, such as array manipulation, string manipulation, and more. In this benchmark, `.uniqBy()` is used to extract unique items from the `workOrders` array based on the `'assigneeId'` property. **Special JS feature or syntax: None** There are no special JavaScript features or syntax being used in this benchmark. **Other alternatives** If you want to implement your own solution without using the `_` library, you could use a simple approach like: ```javascript const uniqueItems = []; for (const workOrder of workOrders) { if (!uniqueItems.find(item => item.assigneeId === workOrder.assigneeId)) { uniqueItems.push(workOrder); } } ``` However, this implementation is likely to be slower than using the `.uniqBy()` function or `Array.prototype.reduce()`, especially for large datasets. **Benchmark preparation code** The script preparation code generates a random array of objects with duplicate `'assigneeId'` values: ```javascript var workOrders = _.range(_.random(1000)).map(x => ({ assigneeId: Math.random().toString(36).slice(3), assigneName: 'LordWarlock' })); ``` The HTML preparation code includes the `_` library's minified version. I hope this explanation helps!
Related benchmarks:
reduce-vs-uniqBy
reduce-vs-uniqBy 1234
uniq-vs-uniqWith
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Comments
Confirm delete:
Do you really want to delete benchmark?