Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript search() vs indexOf()
(version: 0)
Comparing performance of:
search() vs indexOf()
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
search()
const str = 'This is a test'; str.search('test');
indexOf()
const str = 'This is a test'; str.indexOf('test');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
search()
indexOf()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
search()
7803893.0 Ops/sec
indexOf()
212411632.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **What is being tested?** The benchmark compares two methods for searching a string in JavaScript: `str.search('test')` and `str.indexOf('test')`. These two methods are often used interchangeably, but they have different behaviors and performance characteristics. **Options compared:** * `search()`: This method returns the index of the first occurrence of the specified value. If no match is found, it returns -1. * `indexOf()`: This method also returns the index of the first occurrence of the specified value. However, if no match is found, it returns -1 (similar to `search()`), but it can throw an error if the string is non-iterable or null. **Pros and Cons:** * **`search()`**: + Pros: - More readable code, as it explicitly returns the index. - Can be more efficient for older browsers that don't support `indexOf()`. + Cons: - May not be supported in all JavaScript environments (e.g., older versions of Internet Explorer). - Returns -1 if no match is found, which can lead to unexpected behavior. * **`indexOf()`**: + Pros: - More widely supported across modern browsers and JavaScript engines. - Can return -1 without throwing an error. + Cons: - May not be as readable, as it returns a value directly. - Can throw errors in certain situations (e.g., non-iterable strings). **Library usage:** There is no library explicitly mentioned in the benchmark JSON. However, if we were to write this benchmark using a testing framework like Jest or Mocha, we might use libraries like `jsdom` for creating a virtual DOM environment. **Special JS features or syntax:** None are mentioned explicitly, but it's worth noting that these benchmarks assume a vanilla JavaScript execution environment without transpilation or polyfills. If we were to run this benchmark in a more complex environment (e.g., with modern browsers and ES modules), additional libraries or polyfills might be necessary. **Other alternatives:** If you wanted to test alternative methods for searching strings, some options could include: * Using regular expressions (`str.indexOf()` is essentially equivalent) * Implementing your own search algorithm using a loop * Comparing the performance of `str.includes()` (which returns true if the string includes the specified value) against `str.search()` or `str.indexOf()` To write this benchmark, you would need to create a JavaScript script that defines the test cases, then use a testing framework like Jest or Mocha to execute these tests and compare their performance.
Related benchmarks:
index vs lastindexof empty
indexOf vs findIndex with a simple case
String.indexOf vs String.indexOf with the second parameter
String.indexOf(char) vs String.indexOf(char, position)
Comments
Confirm delete:
Do you really want to delete benchmark?