Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.indexOf vs String.indexOf
(version: 0)
Comparing performance of:
Array indexOf vs String indexOf
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['ff1234567890121', 'ff1234567890122', 'ff1234567890123', 'ff1234567890124', 'ff1234567890125']; var s = '-ff1234567890121--ff1234567890122--ff1234567890123--ff1234567890124--ff1234567890125-'; var t = 'ff1234567890124';
Tests:
Array indexOf
var i = a.indexOf(t);
String indexOf
var i = s.indexOf('-' + t + '-');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array indexOf
String 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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array indexOf
32685136.0 Ops/sec
String indexOf
52195964.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two JavaScript functions: `Array.indexOf()` and `String.indexOf()`. Both functions are used to find the index of a specified value in an array or string, respectively. **Options Compared** The benchmark tests two different approaches: 1. **Array.indexOf()**: This function is part of the ECMAScript standard and returns the index of the first occurrence of the specified value in the array. 2. **String.indexOf()**: This function is also part of the ECMAScript standard and returns the index of the first occurrence of the specified value in the string. **Pros and Cons** Here are some pros and cons of each approach: * **Array.indexOf()**: + Pros: Can be used with arrays, relatively simple implementation. + Cons: May have a higher overhead due to its use of a linear search algorithm, which can lead to slower performance for large datasets. * **String.indexOf()**: + Pros: Often faster than `Array.indexOf()` due to optimized implementations in browsers and engines. Also, it can be used with strings, not just arrays. + Cons: May have a higher overhead due to its use of a linear search algorithm, which can lead to slower performance for large datasets. **Library Used** None in this benchmark. The `Array.indexOf()` and `String.indexOf()` functions are part of the ECMAScript standard. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark that would require expertise beyond basic knowledge of JavaScript. **Other Considerations** When choosing between these two approaches, consider the following: * If you're working with arrays and need to find an element quickly, `Array.indexOf()` might be a good choice. * If you're working with strings and need to find a substring or character, `String.indexOf()` might be a better option. **Alternatives** Other alternatives for searching in JavaScript include: * Using regular expressions (`RegExp.test()`, `RegExp.exec()`), which can provide more flexibility but may also introduce overhead. * Using the `some()` and `findIndex()` methods, which are more functional programming-friendly but may have different performance characteristics. * Using optimized libraries like `lodash` or `underscore`, which provide their own implementations of search functions. Keep in mind that these alternatives might not be directly comparable to `Array.indexOf()` and `String.indexOf()`, as they often involve different algorithmic approaches.
Related benchmarks:
Array.indexOf vs String.indexOf vs Object lookup
Array IndexOf vs includes
Array includes vs string indexOf
set.has vs. array.includes vs array.indexOf (string values) - 800 ids
Comments
Confirm delete:
Do you really want to delete benchmark?