Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Uniq by sorting test 2
(version: 0)
Comparing performance of:
Lodash uniqBy vs Custom uniq array
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
var myArr = Array.from({ length: 60000 }, () => ({ value: Math.floor(Math.random() * 1000) })); var myCopy = null;
Tests:
Lodash uniqBy
myCopy = _.uniqBy(myArr, 'value');
Custom uniq array
const itemIdSet = new Set(myArr.map(item => item.value)); const uniqueItems = []; itemIdSet.forEach(value => { const uniqueItem = myArr.find(item => item.value === value); if (uniqueItem) uniqueItems.push(uniqueItem); }); myCopy = uniqueItems;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash uniqBy
Custom uniq array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniqBy
905.2 Ops/sec
Custom uniq array
404.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark test cases and explain what is being tested. **Benchmark Purpose** The goal of this benchmark is to measure the performance of two approaches for removing duplicates from an array while preserving the original order: using the `lodash.uniqBy` function and a custom implementation. **Options Compared** Two options are compared: 1. **Lodash uniqBy**: This approach uses the `lodash.uniqBy` function, which is a utility function that removes duplicate elements from an array based on a key function. In this case, the key function is `'value'`, meaning that it will remove duplicates based on the value of each element in the array. 2. **Custom uniq array**: This approach uses a custom implementation to remove duplicates from the array. It creates a `Set` of unique item IDs and then iterates through the original array to find the corresponding elements for each unique ID. **Pros and Cons** Here are some pros and cons for each approach: * **Lodash uniqBy**: + Pros: Shorter code, easy to understand and maintain, well-tested and widely used library. + Cons: External dependency on Lodash, may have overhead due to the library's size and complexity. * **Custom uniq array**: + Pros: No external dependencies, potentially faster execution since it avoids the overhead of a library function call. + Cons: More complex code, harder to understand and maintain for those not familiar with this implementation. **Library and Its Purpose** In the benchmark, Lodash is used as a utility library to provide a simple way to remove duplicates from an array. The `lodash.uniqBy` function takes two arguments: the input array and a key function that defines how to extract the unique identifier from each element. In this case, the `'value'` key function is used. **Special JS Features or Syntax** There are no special JavaScript features or syntax being used in these benchmark test cases. The code is standard ECMAScript 5+ syntax. **Other Alternatives** If you're interested in exploring alternative approaches to removing duplicates from an array, here are a few options: * Using the `Array.prototype.filter()` method and checking for duplicate values using a `Set`. * Using the `Array.prototype.reduce()` method to accumulate unique elements into a new array. * Using a simple loop and manual iteration through the original array. These alternatives may have different performance characteristics or trade-offs in terms of code complexity, but they can be interesting to explore if you're looking for more options.
Related benchmarks:
Sort lodash vs native
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
lodash uniqBy vs custom uniqBy
Uniq by sorting test
Comments
Confirm delete:
Do you really want to delete benchmark?