Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex & indexOf on 2000 strings
(version: 1)
Comparing performance of:
findIndex vs indexOf
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var _prefix = "test"; var arr = new Array(2000); arr.fill(null); arr = arr.map((el, idx) => _prefix + idx); var rndIdx = Math.floor(Math.random() * 15000); var foo = _prefix + rndIdx;
Tests:
findIndex
var index = arr.findIndex(x => x === foo);
indexOf
var index = arr.indexOf(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
indexOf
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
70788.9 Ops/sec
indexOf
108475.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. The benchmark is designed to measure the performance of two JavaScript methods: `Array.prototype.findIndex` and `Array.prototype.indexOf`. Both methods are used to find the index of a specific element within an array. **FindIndex Method** `findIndex` is called when you want to execute a function on every member of an array, returning the first index at which this value is found or -1 if it is not found. It uses a more efficient algorithm than `indexOf`, especially for large arrays, since it stops searching as soon as it finds the target element. Pros: * More efficient for large arrays * Stops searching as soon as it finds the target element Cons: * May return -1 if the target element is not found, whereas `indexOf` always returns the index of the first occurrence (or -1 if not found) **IndexOf Method** `indexOf` is called when you want to find the index of a specific value within an array. If the value is found, it returns its index; otherwise, it returns -1. Pros: * Always returns the index of the first occurrence * Can be used for arrays with mixed data types Cons: * May be less efficient than `findIndex` for large arrays due to its more extensive search scope **Other Considerations** Both methods use a linear scan approach, which can lead to poor performance for very large arrays. However, their relative efficiency depends on various factors such as array size and the presence of duplicate elements. **Library and Special JS Feature** There is no explicit library mentioned in the benchmark definition or test cases. Both `findIndex` and `indexOf` are built-in JavaScript methods that do not rely on external libraries. However, note that some older versions of Internet Explorer did not support these methods; they were added later to ensure better compatibility across different browsers and platforms. **Alternatives** If you want to compare the performance of your own custom implementation or a third-party library for finding an index in an array, here are some alternatives: * Using `Array.prototype.findIndex` (as shown in the benchmark) * Implementing your own binary search algorithm * Utilizing a library like Lodash's `findIndex` method Keep in mind that each alternative has its pros and cons, depending on factors such as performance requirements, code complexity, and compatibility with different browsers or platforms.
Related benchmarks:
findIndex vs indexOf
findIndex vs indexOf - JavaScript performance
Comparing findIndex with map & indexOf
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?