Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr real
(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 benchmark and explain what's being tested. **Benchmark Overview** The test compares the performance of three string manipulation methods: 1. Regular Expressions (Regex) 2. `.indexOf()` 3. `.startsWith()` with `.substr` All tests use a large dataset of random strings, with each string having a length between 1 and 20 characters. **Script Preparation Code** The script preparation code sets up the test environment by defining a few constants: * `window.regex`: a regex pattern `/^test/` which matches only the exact string "test". * `window.match`: the string "test" to be searched for. * `window.data`: an array of 100,000 random strings generated using the `makeRandomString()` function. * `window.TOTAL_STRINGS`: the total number of strings in the dataset. **Html Preparation Code** The HTML preparation code is empty, which means no additional setup is required before running the tests. **Individual Test Cases** There are three test cases: 1. **Regex**: The script loops through each string in the dataset and applies the regex pattern to match "test" exactly. 2. **.indexOf()**: The script loops through each string in the dataset and checks if "test" is found at index 0 using the `.indexOf()` method. 3. **startsWith() with .substr**: This test case is slightly different, as it uses a combination of methods. It first calls `.startsWith("test")`, which returns true if the string starts with "test". If that's the case, it then calls `.substr(0, 4)` on the string to extract the first four characters. **Library and Special JS Features** In this benchmark, the following libraries/libraries-like-approach are used: * `window.regex`: This is a custom regex pattern defined in the script preparation code. * `.indexOf()`, `.startsWith()`, and `.substr()`: These are built-in JavaScript methods that perform string manipulation. There are no special JS features or syntax used in this benchmark, apart from the custom regex pattern defined earlier. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **Regex**: Pros: * Exact matching. * Can handle complex patterns. Cons: * May be slower than other methods for simple searches. 2. **.indexOf()**: Pros: + Fast and efficient for searching a fixed index. + Works well with large datasets. Cons: + Returns the first occurrence of the substring, not necessarily at index 0. + May be slower than Regex or `startsWith()` for exact matches. 3. **startsWith() with .substr**: This approach is a hybrid that combines two methods. It's faster than Regex but may still be slower than `.indexOf()` for very large datasets. **Other Alternatives** If you're looking for alternative string manipulation methods, consider the following: * `String.prototype.match()`: Similar to Regex, but returns an array of matches or null if no match is found. * `String.prototype.includes()`: Returns true if the string contains the specified substring, without requiring a specific index. * `Array.prototype.indexOf()` and `Array.prototype.lastIndexOf()`: Can be used to find the index of a substring within an array. Keep in mind that performance differences between these methods will depend on the specific use case and dataset size.
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?