Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy vs filter+set
(version: 0)
Comparing performance of:
lodash/uniqBy vs filter+set
Created:
5 years ago
by:
Registered User
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 smalldata = [1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok']; var bigdata = [1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok']; var uniqBy = (arr, fn) => { var dups = new Set(); return arr.filter((v) => { const k = fn(v); if (dups.has(k)) { return false; } dups.add(k); return true; }); };
Tests:
lodash/uniqBy
_.uniqBy(smalldata, (v) => v); _.uniqBy(bigdata, (v) => v);
filter+set
uniqBy(smalldata, (v) => v); uniqBy(bigdata, (v) => v);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash/uniqBy
filter+set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash/uniqBy
190904.4 Ops/sec
filter+set
352976.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Definition** The benchmark is comparing two approaches to remove duplicates from an array: `uniqBy` (using Lodash) and `filter+set`. **What is being tested?** The test cases are running the following code: 1. Using Lodash's `uniqBy` function: ```javascript _.uniqBy(smalldata, (v) => v); _.uniqBy(bigdata, (v) => v); ``` 2. A custom implementation using `filter+set`: ```javascript uniqBy(smalldata, (v) => v); uniqBy(bigdata, (v) => v); ``` The test cases are comparing the execution times of both approaches on two datasets: `smalldata` and `bigdata`. **What options are being compared?** * `uniqBy` (using Lodash): This function uses a Set to keep track of unique values. It iterates through the array, applies the provided function to each value, and returns an array with only the unique values. * `filter+set`: This approach involves creating a Set to store unique values and filtering the original array using the same set. **Pros and Cons** * `uniqBy` (using Lodash): + Pros: - More concise and readable code - Lodash provides a stable way to handle duplicate removal + Cons: - Requires an external library (Lodash) - May have additional overhead due to the library's execution * `filter+set`: + Pros: - No additional dependencies or libraries required - Can be more efficient for large datasets, as it avoids creating a new array with unique values + Cons: - Code can be longer and less readable - Requires manual management of the Set data structure **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks, including string manipulation, array manipulation, and more. The `uniqBy` function in particular is designed to remove duplicates from an array based on a provided function. **Special JS feature or syntax: None mentioned** There are no special features or syntax used in this benchmark. **Other alternatives** * For removing duplicates without using Lodash, you can use the following approaches: + Using `Map` and `Array.from`: Create a Map with unique values and then convert it back to an array. + Using `Set` and `Array.prototype.filter()`: Similar to the `filter+set` approach, but uses `Set` to store unique values and filters the original array. * For removing duplicates while preserving the original order, you can use a different approach, such as using an object with unique keys. I hope this explanation helps!
Related benchmarks:
Lodash filter length vs sumby
lodash/uniq vs filter+set vs array→set→array
unique elements in array using filter v2
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?