Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs substr vs startsWith
(version: 0)
test perf of indexOf vs substr vs startsWith
Comparing performance of:
indexOf vs substr vs startsWith
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = "abcdefghijklmnopqrstuvwxyz";
Tests:
indexOf
return str.indexOf('abcd') == 0
substr
return str.substr(0,4) == 'abcd'
startsWith
return str.startsWith('abcd')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
substr
startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
576612864.0 Ops/sec
substr
546377984.0 Ops/sec
startsWith
572702400.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark definition represents the test case that is being measured. In this case, we have three test cases: 1. `indexOf`: Tests the performance of the `indexOf` method to find the index of a substring in a string. 2. `substr`: Tests the performance of the `substr` method to extract a portion of a string. 3. `startsWith`: Tests the performance of the `startsWith` method to check if a string starts with another string. **Options Compared** The benchmark compares three options: 1. `indexOf` 2. `substr` 3. `startsWith` These methods are all part of the JavaScript String prototype, but they have different use cases and performance characteristics. **Pros and Cons of Each Approach** Here's a brief summary of each approach: * **`indexOf`**: This method is fast because it uses an internal array index to quickly locate the target substring. However, it can be slow if the target substring is not found in the string. + Pros: Fast when target substring is found + Cons: Slow when target substring is not found * **`substr`**: This method creates a new string by extracting a portion of the original string. It's slower than `indexOf` because it involves creating a new string object. + Pros: Creates a new string, which can be useful in some cases + Cons: Slower than `indexOf` * **`startsWith`**: This method checks if a string starts with another string by comparing the two strings using the `===` operator. It's generally faster than `substr` because it avoids creating a new string object. + Pros: Fast and efficient + Cons: May be slower than `indexOf` for large strings **Library Use** In this benchmark, no libraries are used. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you need to measure the performance of other string methods, here are some alternatives: * `includes()`: This method is similar to `indexOf` but checks if the target substring is included anywhere in the string. * `endsWith()`: This method checks if a string ends with another string using a similar approach to `startsWith`. * `split()`, `concat()`, and other string manipulation methods: These methods can be used to create new strings or manipulate existing ones, but they may have different performance characteristics depending on the use case. In summary, this benchmark measures the performance of three common string methods in JavaScript: `indexOf`, `substr`, and `startsWith`. The results indicate that `indexOf` is generally the fastest method when the target substring is found, while `startsWith` is faster than `substr` due to its efficient implementation.
Related benchmarks:
substr vs startsWith
index vs lastindexof startsWith
String indexOf vs startsWith/endsWith
String indexOf() === 0 vs. startsWith()
Comments
Confirm delete:
Do you really want to delete benchmark?