Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.indexOf vs .startsWith vs .substr
(version: 0)
Testing some things
Comparing performance of:
substr vs .indexOf vs .startsWith
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test/; window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
substr
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regex; while (x < TOTAL_STRINGS) { const str = data[x]; str.substr(0, match.length) !== match x += 1; }
.indexOf
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === 0; x += 1; }
.startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.startsWith(match); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
substr
.indexOf
.startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
substr
839.1 Ops/sec
.indexOf
16498.5 Ops/sec
.startsWith
1392.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The MeasureThat.net benchmark tests the performance of three string manipulation methods in JavaScript: `substr`, `.indexOf`, and `.startsWith`. The test generates 100,000 random strings and iterates through them to measure the execution time for each method. **Options Compared** 1. **`substr`**: This method returns a substring from the original string. In this benchmark, it's used to extract a portion of the string, but the starting index is not specified. The test checks if `str.substr(0, match.length) !== match`. 2. **`.indexOf`**: This method returns the index of the first occurrence of a substring in the original string. In this benchmark, it's used to find the index of the match string within each generated string. The test checks if `str.indexOf(match) === 0`, which means finding the exact match at the beginning. 3. **`.startsWith`**: This method returns a boolean indicating whether the original string starts with the specified substring. In this benchmark, it's used to check if each generated string starts with the match string. **Pros and Cons of Each Approach** 1. **`substr`**: * Pros: Can be faster for large strings or specific use cases where only a portion is needed. * Cons: Requires careful indexing, as mentioned in the benchmark code ( `str.substr(0, match.length) !== match` ). 2. **`.indexOf`**: * Pros: Generally fast and efficient for finding exact matches at the beginning of a string. * Cons: May be slower for large strings or cases where the substring is not found. 3. **`.startsWith`**: * Pros: Fast and efficient for simple string checks, as it only requires checking the first few characters. * Cons: May be slower for longer strings or more complex checks. **Library Used** None are explicitly mentioned in this benchmark. However, `regex` is used to define a regular expression pattern (`/^test/`), which is not directly related to string manipulation but rather serves as a preparation step. **Special JS Feature/Syntax** There are no specific JavaScript features or syntax used in this benchmark beyond the standard methods being tested (`.substr`, `.indexOf`, and `.startsWith`). The focus is on measuring the performance of these built-in methods. **Other Alternatives** Some alternative approaches to string manipulation might include: * Using a different method, such as `slice()` or `substring()` * Utilizing a library like Lodash for more complex string operations * Employing regular expressions with groups (e.g., `(.*?)(?!...)`) for matching and extracting substrings Keep in mind that the performance differences between these methods may vary depending on the specific use case, JavaScript engine, and platform.
Related benchmarks:
.startsWith vs .charAt for single character
IndexOf vs startwith
indexOf vs startsWith
.substr vs .startwith
.startsWith vs .charAt vs str[0] for single character
Comments
Confirm delete:
Do you really want to delete benchmark?