Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.indexOf vs .startsWith
(version: 0)
Testing some things
Comparing performance of:
.startsWith vs .indexOf
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:
.startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.startsWith(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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.startsWith
.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 SberBrowser/35.0.0.0
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.startsWith
159.2 Ops/sec
.indexOf
136.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark tests two different string manipulation methods: `String.prototype.startsWith()` and `String.prototype.indexOf()`. We'll break down what's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition JSON** The benchmark definition JSON represents a script that sets up the environment for the test. It defines: 1. Variables: * `window.regex`: a regular expression object set to `/^test/`. * `window.match`: a string variable set to `"test"`. * `window.data`: an array of strings, initialized with 100,000 random strings. * `TOTAL_STRINGS`: a constant set to the length of the `window.data` array. 2. Functions: * `getRandomInt(max)`: generates a random integer between 0 and `max`. * `makeRandomString(len)`: generates a random string of length `len`. **Individual Test Cases** There are two test cases: 1. `.startsWith` 2. `.indexOf` Each test case has a benchmark definition JSON that defines the script to be executed. **Test Case: .startsWith** The script checks each string in the `window.data` array using the `startsWith()` method and verifies that it returns true for the expected input `"test"`. The test case uses the `window.match` variable as the expected value. **Pros and Cons of .startsWith()** * **Pros:** + Efficient, since it can stop searching as soon as it finds a match. + Fewer comparisons required compared to `.indexOf()`. * **Cons:** + May be slower for large strings due to the overhead of string comparison. **Test Case: .indexOf** The script checks each string in the `window.data` array using the `.indexOf()` method and verifies that it returns 0 when searching for the expected input `"test"`. The test case uses the `window.match` variable as the expected value. **Pros and Cons of .indexOf()** * **Pros:** + Can be faster than `.startsWith()` for large strings since it can return early. * **Cons:** + May require more comparisons to find the exact index, which can be slower. **Library Use** The benchmark uses the `window.match` variable, which is set to a string value `"test"`. This suggests that the library being tested is the `String.prototype.startsWith()` method. The purpose of this library is to check if a string starts with a specified substring. **Special JS Features or Syntax** None are explicitly mentioned in the benchmark code. **Other Alternatives** If you need to implement your own string manipulation methods, here are some alternatives: * For checking if a string starts with a substring: use a loop that iterates over each character of the string. * For checking if a string contains a specific substring: use a loop that searches for the substring within the string. Keep in mind that these implementations will likely be slower and less efficient than using built-in methods like `.startsWith()` or `.indexOf()`.
Related benchmarks:
.startsWith vs .charAt for single character
IndexOf vs startwith
indexOf vs startsWith
.indexOf vs .startsWith - cccccccccccccccccccc
.startsWith vs .charAt vs str[0] for single character
Comments
Confirm delete:
Do you really want to delete benchmark?