var arr = [];
var i = 0;
while (i <= 1E5) arr[i] = i++;
const item = arr.find(item => item == 1E5);
const index = arr.findIndex(item => item == 1E5);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.find | |
Array.prototype.findIndex |
Test name | Executions per second |
---|---|
Array.prototype.find | 695.0 Ops/sec |
Array.prototype.findIndex | 713.7 Ops/sec |
Let's dive into the benchmark and explore what's being tested.
What is tested?
The provided JSON represents two test cases: Array.prototype.find
and Array.prototype.findIndex
. Both tests are comparing the performance of these two methods on an array prototype, specifically when searching for a value equal to 1E5 (100,000).
Options compared
In this benchmark, only two approaches are being compared:
item == 1E5
). If no such element exists, it returns undefined.item == 1E5
). If no such element exists, it returns -1.Pros and Cons of each approach
Here's a brief summary:
Library and purpose
Neither find
nor findIndex
relies on any external libraries. They are both built-in methods of the JavaScript Array prototype.
Special JS feature or syntax
None mentioned explicitly, but keep in mind that modern JavaScript engines often optimize these methods for performance.
Other alternatives
If you wanted to test alternative approaches, you might consider:
arr.forEach
or a simple iteration).some
, every
, or includes
.However, these alternatives would not be directly comparable to the built-in find
and findIndex
methods, as they would likely have different performance characteristics and use cases.