Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr (Such)
(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):
Let's break down the provided benchmark and explain what is being tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The test case measures the performance of three different string comparison methods: 1. **Regex**: Using a regular expression to search for a pattern in a string. 2. **.indexOf**: Finding the index of a substring within a string using the `indexOf` method. 3. **.startsWith**: Checking if a string starts with a specified prefix using the `startsWith` method. **Script Preparation Code** The script preparation code sets up some global variables: * `window.regex`: A regular expression object defined as `/^test/`. * `window.match`: The string "test" that will be searched for in the strings. * `window.data`: An array of 100,000 random strings generated using a combination of uppercase and lowercase letters, digits, and special characters. * `TOTAL_STRINGS`: A constant set to 100,000. **Html Preparation Code** The HTML preparation code simply contains an empty `<div>` element, which is likely used as a container for the benchmark results. **Options Compared** The three options being compared are: 1. **Regex**: Using a regular expression to search for the `match` string within each string in the `data` array. 2. **.indexOf**: Finding the index of the `match` string within each string in the `data` array using the `indexOf` method. 3. **.startsWith**: Checking if each string in the `data` array starts with the `match` string using the `startsWith` method. **Pros and Cons** Here's a brief summary of the pros and cons of each option: 1. **Regex**: * Pros: Can be more flexible than other methods, can perform complex pattern matching. * Cons: Can be slower due to the overhead of compiling regular expressions, may require more resources for larger strings. 2. **.indexOf**: * Pros: Fast and efficient, doesn't require compiling regular expressions or performing complex pattern matching. * Cons: May not work correctly for certain patterns (e.g., edge cases), can be slower than Regex for small strings. 3. **.startsWith**: * Pros: Simple and fast, always returns a boolean result. * Cons: May not work correctly for certain use cases (e.g., checking if a string contains a specific substring), can be slower than .indexOf for large strings. **Other Considerations** * The benchmark only tests these three options on a single string comparison scenario. In real-world scenarios, you may need to test these methods on different types of strings and patterns. * The `TOTAL_STRINGS` constant is set to 100,000, which may be too small for some systems. You can adjust this value to better simulate real-world usage. **Alternative Methods** Some alternative string comparison methods that are not included in the benchmark include: 1. **.includes**: Similar to .indexOf, but returns a boolean result indicating whether the specified string is included in the original string. 2. **.endsWith**: Checks if a string ends with a specified suffix using the `endsWith` method. 3. **String.prototype.includes()**: A modern method for checking if a substring exists within a string. Note that these alternative methods may have slightly different performance characteristics or use cases compared to the options being tested in the benchmark.
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?