Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy vs findIndex
(version: 0)
Comparing performance of:
uniqBy vs findIndex
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: 40}, () => Math.floor(Math.random() * 40)); var myCopy = null;
Tests:
uniqBy
myCopy = _.uniqBy(MyArr);
findIndex
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
findIndex
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
663582.2 Ops/sec
findIndex
3316585.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition JSON** The benchmark is testing two approaches to removing duplicates from an array: `uniqBy` using Lodash's `uniqBy` function, and a custom implementation using `findIndex`. **Script Preparation Code** ```javascript var MyArr = Array.from({length: 40}, () => Math.floor(Math.random() * 40)); var myCopy = null; ``` This code creates an array `MyArr` with 40 random elements. The `myCopy` variable is initialized but not used anywhere in the benchmark, which seems like a mistake. **Html Preparation Code** ```javascript <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` This code includes the Lodash library, which provides the `uniqBy` function being tested. **Individual Test Cases** 1. **uniqBy** The benchmark definition is: ```javascript myCopy = _.uniqBy(MyArr); ``` This uses Lodash's `uniqBy` function to remove duplicates from `MyArr`. The `_.uniqBy` function takes an array and a function that defines the unique identifier for each element. Lodash's `uniqBy` is a useful utility function that allows you to easily remove duplicates based on any property of an object. It returns a new array with unique elements, preserving the original order. Pros: * Easy to use: simply pass in the array and the unique identifier function. * Fast performance: optimized for large arrays. Cons: * Dependencies Lodash: requires including the library in your HTML file. * May not be suitable for all data formats (e.g., arrays of objects with nested properties). 2. **findIndex** The benchmark definition is: ```javascript myCopy = myCopy.filter( (dependentNode, index, dependentNodesArr) => dependentNodesArr.findIndex((currentNode) => currentNode.id === dependentNode.id) === index ); ``` This custom implementation uses `filter` to remove duplicates from `MyArr`. It checks each element against every other element in the array using `findIndex`, which returns the index of the first element that matches a given condition. The comparison is done using `===` for strict equality. Pros: * No dependencies: no additional libraries are required. * Simple implementation: only uses built-in functions. Cons: * Performance: this approach has higher overhead due to the nested loops and function calls. * Error-prone: if the unique identifier function is not correctly implemented, it may produce incorrect results or throw errors. **Other Considerations** When choosing between `uniqBy` and a custom implementation like `findIndex`, consider the following factors: * Performance: Lodash's `uniqBy` is generally faster than a custom implementation due to its optimized algorithm. * Maintainability: if you need to modify the unique identifier function, Lodash's `uniqBy` makes it easy to do so without changing the underlying code. * Dependencies: if you want to avoid including additional libraries in your project, a custom implementation may be preferred. **Alternatives** Other alternatives for removing duplicates from an array include: * Using a data structure like a Set or Map, which automatically removes duplicates and has fast lookup times. * Using a sorting algorithm followed by a `reduce` function to remove duplicates. * Implementing a hash-based approach using a library like `fast-hash`. However, these alternatives may have different trade-offs in terms of performance, maintainability, and dependencies.
Related benchmarks:
Lodash uniqBy vs Set
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Lodash uniqBy vs Javascript uniqBy
uniqBy vs uniqByFindIndex
Comments
Confirm delete:
Do you really want to delete benchmark?