Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs _sortedIndexBy
(version: 0)
Comparing performance of:
sortedIndexBy() vs findIndex()
Created:
4 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 arr = []; for(let i = 0; i < 5000; i++){ arr.push({price: i}) }
Tests:
sortedIndexBy()
_.sortedIndexBy(arr, {price: 4242}, 'price')
findIndex()
arr.findIndex((o) => o.price === 4242)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sortedIndexBy()
findIndex()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sortedIndexBy()
7224369.0 Ops/sec
findIndex()
313086.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test on the MeasureThat.net website. The benchmark is designed to compare two approaches: `sortedIndexBy` from Lodash and `findIndex`. Both functions aim to find the index of an element in an array that matches a specific condition. **Options Compared** * **Lodash's sortedIndexBy**: This function uses the `lodash.sortby` method, which returns the index of the first element in the array that satisfies the provided function. The function takes three arguments: the input array, an object with the key to use for sorting, and a string indicating the direction of the sort. * **Array.prototype.findIndex**: This is a built-in JavaScript method that returns the index of the first element in the array that satisfies the provided condition. **Pros and Cons** * **Lodash's sortedIndexBy**: * Pros: * More robust error handling: If the input array is empty or not iterable, `sortedIndexBy` will throw an error with a descriptive message. * Can handle more complex sorting conditions. * Cons: * Additional library dependency (Lodash). * May have overhead due to the additional function call and object creation. * **Array.prototype.findIndex**: * Pros: * Lightweight: No additional library dependencies or overhead. * Built-in method, so it's likely optimized for performance by JavaScript engines. * Cons: * Can throw errors if the input array is empty or not iterable. * May have performance issues if the array is very large and the condition is computationally expensive. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like string manipulation, data transformation, and more. The `sortedIndexBy` function is part of Lodash's `lodash.sortby` method, which allows sorting an array by multiple criteria and returning the index of the first element that matches a specific condition. **Special JS Feature or Syntax** There doesn't appear to be any special JavaScript features or syntax used in this benchmark. The code relies on standard JavaScript constructs like loops, arrays, and object properties. **Alternatives** If you need to find an index in an array without using `Array.prototype.findIndex`, some alternatives include: * **Using a custom loop**: You can write a simple loop that iterates through the array, comparing each element to the target value. * **Using the `indexOf` method**: If your browser supports it, you can use the `indexOf` method to find the index of an element in the array. Note that `indexOf` returns -1 if the element is not found. * **Using a library like Lodash (other methods)**: Depending on the complexity of your requirements, you may want to explore other Lodash functions like `findIndexBy`, which provides similar functionality to `sortedIndexBy`. In summary, the benchmark compares two approaches for finding an index in an array that matches a specific condition. While both options have their pros and cons, `Array.prototype.findIndex` is a lightweight built-in method that may be suitable for simple use cases, while Lodash's `sortedIndexBy` provides more robust error handling and can handle complex sorting conditions at the cost of additional library dependencies and potential overhead.
Related benchmarks:
sortedIndex vs Loops
findIndex performance
lodash _sortedIndex vs native indexOf
_.findIndex vs native findIndex
Comments
Confirm delete:
Do you really want to delete benchmark?