Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr sdfg sdf gsd
(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):
**Overview** The provided JSON represents a JavaScript microbenchmark test case for measuring the performance of different string comparison methods: regular expressions (`regex`), `indexOf`, and `startsWith`. The test case creates an array of random strings, iterates over them, and applies each comparison method to verify if a specific pattern exists in the string. **Benchmarked Options** The three options being compared are: 1. **Regular Expressions (`regex`)**: A pattern-based search that allows for regular expression patterns to be used for matching. 2. **`indexOf`**: A method that returns the index of the first occurrence of a substring within another string. 3. **`startsWith`**: A method that returns a boolean indicating whether a string starts with another string. **Pros and Cons** * **Regular Expressions (`regex`)**: + Pros: flexible, powerful, and supports complex patterns. + Cons: slow performance due to the overhead of compiling and executing regular expression patterns. * `indexOf`: + Pros: simple, fast, and widely supported. + Cons: may not work correctly for edge cases (e.g., empty strings) or Unicode characters. * `startsWith`: + Pros: fast, efficient, and suitable for most use cases. + Cons: limited to checking if a string starts with another; does not support substring searches. **Libraries and Special JS Features** The test case uses the following library: * None explicitly mentioned, but it assumes that the `window` object is available, which suggests using a browser or Node.js environment. There are no special JavaScript features used in this benchmark. However, note that the use of `RegExp` (regular expression) would be denoted by the `R` symbol, not shown here. **Other Alternatives** For string comparison methods, other alternatives to consider: * `includes`: A method introduced in ECMAScript 2015 (ES6+) that returns a boolean indicating whether a string includes another substring. * `contains`: Not a standard JavaScript method; consider using `includes` instead. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the options being benchmarked.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
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?