Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .startsWith
Created:
8 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:
Regex
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]; regex.test(str); 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) === true; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different methods for checking if a string starts with a specific substring: * **Regex:** This method uses a regular expression (`/^test/`) to match the beginning of each string in the dataset. * **`.indexOf`:** This method uses the `indexOf()` function to check if the target substring (`'test'`) is at index 0 within each string. * **`.startsWith`:** This method directly utilizes the built-in `startsWith()` function to determine if a string starts with the specified substring (`'test'`). **Pros and Cons:** * **Regex:** * **Pro:** Highly flexible for complex patterns, can be optimized using flags. * **Con:** Can be slower than simpler methods due to its nature of parsing and matching against a pattern. * **`.indexOf`:** * **Pro:** Relatively simple and straightforward implementation. * **Con:** Less performant than `.startsWith` in modern JavaScript engines. * **`.startsWith`:** * **Pro:** Specifically designed for this task, often the most efficient option in recent browsers. * **Con:** Less flexible compared to regex if you need complex pattern matching. **Alternatives:** While these are common approaches, there might be other scenarios where a different method could be more suitable: * **For very specific cases with known string patterns:** Manually crafted string comparisons or bitwise operations could be surprisingly efficient. * **If the target substring is frequently used:** Pre-checking and storing results in a lookup table can provide significant performance gains. **Important Notes:** The benchmark results provided will vary depending on factors like browser, JavaScript engine version, and hardware. They offer a general idea of relative performance but should not be considered definitive for all situations.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?