Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex (Array prototype methods) 123123
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.findIndex vs Array.prototype.findIndex (rev)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; var arrRev = []; var j = 0; var SIZE = 1E5; var middle = Math.floor(SIZE / 2); while (i <= SIZE) arr[i] = i++; while (j <= SIZE) arrRev[j] = j++;
Tests:
Array.prototype.findIndex
const index = arr.findIndex(item => item == middle);
Array.prototype.findIndex (rev)
const index = arrRev.length - arrRev.reverse().findIndex(item => item == middle) - 1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.findIndex
Array.prototype.findIndex (rev)
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/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.findIndex
171.9 Ops/sec
Array.prototype.findIndex (rev)
174.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is designed to measure which approach is faster: using `Array.prototype.findIndex` directly or using `Array.prototype.reverse()` followed by `findIndex`. The script preparation code generates two arrays, one ascending (`arr`) and one descending (`arrRev`), both of size 100,000. These arrays are used as the input for the test cases. **Options Compared** The two options being compared are: 1. **Array.prototype.findIndex**: This method searches for the first element in an array that satisfies a provided testing function. If no elements satisfy the testing function, -1 is returned. 2. **Array.prototype.reverse().findIndex**: This approach reverses the array using `Array.prototype.reverse()`, then uses `findIndex` to search for the target value. **Pros and Cons of Each Approach** * **Array.prototype.findIndex**: + Pros: Can be more efficient because it stops searching as soon as it finds a match, reducing unnecessary comparisons. + Cons: May not be faster in all cases due to its dynamic nature and potential overhead. * **Array.prototype.reverse().findIndex**: + Pros: Guaranteed to find the target value in the reversed array, which might lead to better performance in some cases. Also, reversing the array may happen before searching, reducing the number of comparisons needed. + Cons: Requires extra memory to store the reversed array and can be slower due to the additional overhead of `Array.prototype.reverse()`. **Library/Utility** There is no specific library or utility used in this benchmark beyond what comes with JavaScript. However, note that modern browsers like Chrome implement various performance-related features, such as just-in-time (JIT) compilation and caching, which may influence the results. **Special JS Features/Syntax** The benchmark does not specifically target any special JavaScript features or syntax. It primarily focuses on demonstrating the performance difference between two common array methods. **Other Alternatives** Other alternatives for finding an element in an array could be: 1. **Array.prototype.indexOf()**: Similar to `findIndex`, but returns -1 if no element is found, rather than a negative number. 2. **Array.prototype.findIndex() with callback function optimization**: Some JavaScript engines, like V8 (used by Chrome), have optimized the behavior of `findIndex()` for certain types of callbacks. 3. **Native implementation in other languages**: Depending on the specific requirements and performance needs, other programming languages might offer native implementations or alternatives to `Array.prototype.findIndex`. Keep in mind that these alternatives are not being tested here, but rather serve as a general overview of potential approaches.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) 22
find vs findIndex (Array prototype methods) stop at first found
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?