Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs _sortedIndexBy vs custom sortedIndexBy
(version: 0)
Comparing performance of:
lodash sortedIndexBy() vs native findIndex() vs custom sortedIndexBy()
Created:
4 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 arr = []; for(let i = 0; i < 5000; i++){ arr.push({ price: i }) } function sortedIndexBy(array, comparator) { let min = 0 let max = array.length let index = Math.floor((min + max) / 2) while (max > min) { if (comparator(array[index]) < 0) { max = index } else { min = index + 1 } index = Math.floor((min + max) / 2) } return index - 1 }
Tests:
lodash sortedIndexBy()
_.sortedIndexBy(arr, {price: 4242}, 'price')
native findIndex()
arr.findIndex((o) => o.price === 4242)
custom sortedIndexBy()
sortedIndexBy(arr, (o) => (4242 > o.price ? 1 : (4242 < o.price ? -1 : 0)))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash sortedIndexBy()
native findIndex()
custom sortedIndexBy()
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/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash sortedIndexBy()
3175069.8 Ops/sec
native findIndex()
285171.7 Ops/sec
custom sortedIndexBy()
725579.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of microbenchmarks! **What is tested?** The provided JSON represents three test cases that compare the performance of three different approaches to find an index in an array: 1. **Native `findIndex()`**: A built-in JavaScript method that finds the index of a specific value in an array. 2. **Lodash's `_sortedIndexBy()`**: A utility function from the Lodash library that finds the index of a specific value in a sorted array based on a custom comparator. 3. **Custom `sortedIndexBy()`**: A custom implementation of the sorted index by algorithm. **Options compared** The three test cases compare the performance of these three approaches to find an index in an array with 5000 elements, where each element has a unique price property. **Pros and cons of each approach:** 1. **Native `findIndex()`**: * Pros: Built-in JavaScript method, efficient for small arrays. * Cons: May not perform well for large arrays or when the array is sorted in a specific order (e.g., ascending). 2. **Lodash's `_sortedIndexBy()`**: * Pros: Optimized for finding indices in sorted arrays, can be used with custom comparators. * Cons: Requires an external library, may not be necessary for small or simple use cases. 3. **Custom `sortedIndexBy()`**: * Pros: Can be optimized for specific use cases (e.g., when the array is already sorted), can be implemented in pure JavaScript without dependencies. * Cons: May require more development time and effort, may not be as efficient as native methods or Lodash's implementation. **Library:** The `_sortedIndexBy()` function from Lodash is a utility function that finds the index of a specific value in a sorted array based on a custom comparator. It uses a binary search algorithm to achieve optimal performance. **Special JS feature or syntax:** None mentioned in this benchmark. **Benchmark preparation code explanation** The `script preparation code` defines an array `arr` with 5000 elements, each with a unique price property. The `sortedIndexBy()` function is then defined, which uses a binary search algorithm to find the index of a specific value in the sorted array. The `Html Preparation Code` includes a script tag that loads the Lodash library. **Other alternatives:** For finding an index in an array without using native methods or Lodash's implementation, developers can consider other approaches: 1. **Manual binary search**: Implementing a custom binary search algorithm to find the index of a value in a sorted array. 2. **Using a different data structure**: Consider using a more efficient data structure, such as a balanced binary search tree, for faster lookup times. However, these alternatives may come with additional development time and effort, and may not offer significant performance benefits over native methods or Lodash's implementation.
Related benchmarks:
Lodash sort vs array.prototype.sortsr23ijo23rf fs a
Lodash sort vs array.prototype.sort vs basic sort vs Stable Sort 2
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?