Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy vs uniqByFindIndex
(version: 0)
Comparing performance of:
uniqBy vs uniqByFindIndex
Created:
2 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: 200}, () => ({id: Math.floor(Math.random() * 40)})); var myCopy = null;
Tests:
uniqBy
myCopy = _.uniqBy(myArr, 'id');
uniqByFindIndex
myCopy = myCopy.filter( (dependentNode, index, dependentNodesArr) => dependentNodesArr.findIndex( (currentNode) => currentNode.id === dependentNode.id ) === index )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniqBy
uniqByFindIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqBy
113837.7 Ops/sec
uniqByFindIndex
633522.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of two approaches to find unique elements in an array: 1. `_.uniqBy(myArr, 'id')` - This uses the Lodash library's `uniqBy` function, which returns a new array with duplicate elements removed based on a specified value. 2. `(myArr.filter((dependentNode, index, dependentNodesArr) => dependentNodesArr.findIndex((currentNode) => currentNode.id === dependentNode.id) === index))` - This is an implementation of the `uniqByFindIndex` algorithm, which uses a filter function to remove duplicate elements based on their indices. **Options Compared** The benchmark compares two approaches: * Lodash's `uniqBy` function * A custom implementation using `filter` and `findIndex` **Pros and Cons** **Lodash's `uniqBy`** Pros: * Concise and expressive syntax * Well-tested and widely adopted library * Provides a reliable and efficient solution Cons: * Adds an external dependency (the Lodash library) * May not be optimal for very large datasets due to its overhead **Custom Implementation** Pros: * No external dependencies * Can be optimized for performance, especially for large datasets Cons: * More verbose syntax * Requires manual handling of edge cases and potential errors **Other Considerations** * The benchmark assumes that the array elements have an `id` property. If this is not the case, the implementation may need to be adjusted. * The custom implementation uses a filter function with a callback function. This can be less efficient than using Lodash's `uniqBy` for large datasets. **Library and Purpose** Lodash (version 4.17.5) is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object manipulation, and more. The `uniqBy` function specifically returns a new array with duplicate elements removed based on a specified value. **Special JS Features or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that some modern browsers may have performance optimizations or specific implementations of the `filter` and `findIndex` functions that could affect the results of this benchmark. **Other Alternatives** If you're looking for alternative approaches to find unique elements in an array, you might consider: * Using a Set data structure: You can convert the array to a Set (which automatically removes duplicates) and then iterate over it. * Using a Map data structure: Similar to using a Set, but with additional benefits like preserving key-value pairs. * Implementing a custom algorithm using only basic JavaScript functions (e.g., `forEach`, `map`, etc.). * Using a library like Moment.js for date-based uniqueness (if applicable). Keep in mind that the best approach will depend on your specific use case and requirements.
Related benchmarks:
Lodash uniqBy vs Set
Lodash uniqBy vs Set 10000
Unique lodash vs vanilla
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
New set vs UniqWith
Comments
Confirm delete:
Do you really want to delete benchmark?