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
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = new RegExp("^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.startsWith(match); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.indexOf
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):
I'll provide an explanation of the provided JSON, benchmark options, and considerations. **Benchmark Definition JSON** The `benchmark.json` file defines two test cases: 1. **`Regex`**: Tests the performance of using a regular expression (`regex`) to check if a string (`str`) matches a pattern. 2. **`.indexOf`**, **`.startsWith`**, and **`.substr`**: Tests the performance of using the `.indexOf`, `.startsWith`, and `.substr` methods, respectively, on strings. **Options Compared** The benchmark compares the performance of: 1. **Regular Expressions (`regex`)**: Using a regular expression to search for a pattern in a string. 2. **`.indexOf`**: Using the `.indexOf` method to find the index of a substring within another string. 3. **`.startsWith`**: Using the `.startsWith` method to check if a string starts with another string. 4. **`.substr`**: Using the `.substr` method to extract a subset of characters from a string. **Pros and Cons** * **Regular Expressions (`regex`)**: * Pros: flexible pattern matching, support for character classes, anchors, and quantifiers. * Cons: slower performance due to the complexity of pattern matching. * **`.indexOf`**, **`.startsWith`**, and **`.substr`**: * Pros: faster performance since they are optimized for simple substring searches. * Cons: limited flexibility and support for complex patterns. **Library Used** None explicitly mentioned, but it's likely that the `RegExp` constructor is being used to create a regular expression object. **Special JS Feature or Syntax** No special JavaScript features or syntax are explicitly mentioned in this benchmark. **Other Considerations** * **Data Generation**: The benchmark generates random strings using a predefined character set (`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`) to simulate real-world string data. * **Looping**: Both test cases use a `while` loop to iterate over the generated strings, ensuring that each string is processed multiple times. **Alternative Benchmarks** Other benchmarks could be used to compare these options: 1. **`.includes`**: Using the `.includes` method instead of `.indexOf`. 2. **String.prototype.match()**: Using the `match()` method instead of regular expressions. 3. **Array.prototype.includes()**: Using the `includes()` method on arrays instead of strings. These alternatives would provide additional insights into the performance characteristics of each option in different contexts.
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?