Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr x
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .startsWith
Created:
6 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); 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
llama3.2:3b
, generated one year ago):
The provided benchmark tests the performance of three different string manipulation methods in JavaScript: regular expressions, `indexOf()`, and `startsWith()`. **Regular Expressions** The test case for regular expressions uses the `test()` method to search for a pattern (`/^test/`) within a large array of randomly generated strings. The `window.regex` variable is defined before the benchmark runs, so it's already set up for this specific use case. Pros: * Regular expressions can be powerful and flexible tools for string matching. * They can handle complex patterns and cases (e.g., `.match()` can extract all matches from a string). Cons: * Regular expressions can be slower than other methods, especially for simple searching tasks. * They require more characters to write and maintain compared to other methods. **.indexOf()** The test case for `indexOf()` searches for a specific string (`"test"`) within the same large array of randomly generated strings. The `window.match` variable is defined before the benchmark runs, so it's already set up for this specific use case. Pros: * `indexOf()` is generally faster than regular expressions. * It's a simple and straightforward method to find a specific string within an array. Cons: * `indexOf()` returns `-1` if the string is not found, which might lead to unexpected behavior in some cases. * It only searches for an exact match. **.startsWith()** The test case for `startsWith()` checks whether a string starts with a specific pattern (`"test"`). The same large array of randomly generated strings is used as before. Pros: * `startsWith()` is generally faster than regular expressions and `indexOf()`. * It's a simple method to check if a string matches a specific prefix. Cons: * It only checks for an exact match at the beginning of the string. * It might return false positives (e.g., `"test"`, but not "test2"`). **Library usage** In this benchmark, two libraries are used: 1. **Regular Expressions**: The `^` character in the regular expression pattern is used to indicate the start of the search. This library is built-in to JavaScript. 2. **String Methods (`indexOf()`, `startsWith()`)**: These methods are native to JavaScript and do not require any external libraries. **Special JS features** There are no special JavaScript features or syntaxes explicitly mentioned in this benchmark. However, some edge cases might be worth noting: * String concatenation: The test data is generated by concatenating strings together using the `+` operator. * Window object manipulation: The benchmark uses the `window` object to define variables and execute code. **Other alternatives** Some alternative string manipulation methods that could be used in a similar benchmark include: 1. **ES6 template literals**: Could be used for simple string interpolation, but might not offer significant performance advantages over native methods. 2. **String.prototype.replace()**: Could be used for more complex string transformations, but might introduce additional overhead compared to `indexOf()` and `startsWith()`. 3. **Array.prototype.reduce()**: Could be used to perform custom string manipulations, but would likely require more code and potentially slower performance. In summary, the provided benchmark tests the performance of regular expressions, `indexOf()`, and `startsWith()` in JavaScript, highlighting their pros and cons for different use cases.
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?