Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Set vs custom uniqBy
(version: 0)
Comparing performance of:
Lodash uniqBy vs Set vs custom uniqBy
Created:
3 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 MyArr = Array.from({length: 40}, () => Math.floor(Math.random() * 40)); var myCopy = null; function uniqBy(arr, predicate) { const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate]; return [...arr.reduce((map, item) => { const key = (item === null || item === undefined) ? item : cb(item); map.has(key) || map.set(key, item); return map; }, new Map()).values()]; };
Tests:
Lodash uniqBy
myCopy = _.uniqBy(MyArr);
Set
myCopy = [...new Set(MyArr)]
custom uniqBy
myCopy = uniqBy(MyArr, v => v)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash uniqBy
Set
custom 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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing three approaches for removing duplicates from an array: 1. **Lodash `uniqBy`**: A function that removes duplicate elements from an array based on a provided predicate function. 2. **Set**: A built-in JavaScript data structure that automatically removes duplicates when used as an array. 3. **Custom `uniqBy`**: A custom implementation of the `uniqBy` function. **Options Compared** The benchmark is comparing three options: * Lodash's `uniqBy` function * The built-in `Set` data structure * A custom implementation of `uniqBy` **Pros and Cons** 1. **Lodash `uniqBy`**: * Pros: Easy to use, well-documented, and optimized for performance. * Cons: Requires including the Lodash library in your codebase, which may add unnecessary size and complexity. 2. **Set**: * Pros: Lightweight, easy to understand, and uses built-in JavaScript functionality. * Cons: May not be as efficient as custom implementations or libraries like Lodash for large datasets. 3. **Custom `uniqBy`**: * Pros: Can be optimized for specific use cases or performance requirements. * Cons: Requires manual implementation, which can add complexity and maintainability issues. **Library: Set** The built-in `Set` data structure is used in the "Set" benchmark case. A Set is a collection of unique values that automatically removes duplicates when items are added or removed. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax being tested in this benchmark. **Other Alternatives** If you're looking for alternative approaches, consider: * Using the `filter()` method with a predicate function to remove duplicates. * Implementing a custom sorting and grouping algorithm using array methods like `sort()`, `map()`, and `reduce()`. * Using a library like Ramda or Immer.js, which offer optimized implementations of functional programming techniques. When choosing an approach, consider factors such as performance requirements, code complexity, and maintainability. The Lodash `uniqBy` function is often a good default choice due to its ease of use and optimization, but custom implementations may be necessary for specific use cases or performance requirements.
Related benchmarks:
lodash uniq vs native uniq
lodash uniq vs Map
lodash uniq vs Map2
lodash uniqBy vs custom uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?