Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
executable indexOf vs findIndex when using a primitive types vs when using an object
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [{number: 'one'}, {number:'two'}, {number: 'three'}]; var arr2 = ['one', 'two', 'three'];
Tests:
findIndex
arr.findIndex(el => el.number === 'one')
indexOf
arr2.indexOf('one')
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested in this benchmark. **What is being tested?** The test case compares the performance difference between `indexOf` and `findIndex` methods when searching for a primitive value (`'one'`) versus an object property (`number === 'one'`) within two different arrays: one with object elements (`arr`) and another with primitive elements (`arr2`). **Options being compared** Two options are being compared: 1. **`indexOf`**: This method searches for the specified value in the array and returns its index if found, or -1 otherwise. 2. **`findIndex`**: This method is similar to `indexOf`, but it only returns the index of the first occurrence of the specified value. **Pros and cons of each approach** * **`indexOf`**: + Pros: generally faster and more straightforward implementation. + Cons: may return -1 if the value is not found, which can be misleading in some cases. Additionally, it only searches for exact matches. * **`findIndex`**: + Pros: returns the index of the first occurrence of the specified value, even if subsequent occurrences are present. + Cons: can be slower than `indexOf` because it requires iterating over the array until a match is found. **Library usage** There doesn't appear to be any specific library being used in this benchmark. The script uses standard JavaScript functions and data structures (arrays and objects). **Special JS feature or syntax** No special JavaScript features or syntax are mentioned that would require additional explanation. However, it's worth noting that `findIndex` is a part of the ECMAScript 2015 (ES6) standard, so its use may indicate that the benchmark is running on a relatively modern JavaScript engine. **Other alternatives** While not explicitly mentioned in the benchmark, other ways to achieve similar results could include: * Using regular expressions (` RegExp.prototype.indexOf()` or `RegExp.prototype.exec()` ) * Implementing a custom search function using loops and conditional statements. * Using a library like Lodash, which provides the `findIndex` method as part of its utility functions. Overall, this benchmark is designed to compare the performance differences between two specific array methods in JavaScript, with different types of arrays (object vs. primitive) being used to test their behavior.
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf for simple array 2
indexOf vs findIndex simple X
Array find with indexOf vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?