Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .startsWith vs .indexOf vs .substr on string length 2000
(version: 0)
Comparing performance of:
regex start vs startsWith vs indexOf start vs substr
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'test'; window.matchLength = window.match.length; 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(2000)); }
Tests:
regex start
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexStart; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); 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) === 0; x += 1; }
indexOf start
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; }
substr
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.substr(0, match.length) === match; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex start
startsWith
indexOf start
substr
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):
**Benchmark Explanation** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of four string comparison methods: regular expressions (`regex`), `startsWith`, `indexOf`, and `substr`. The test data consists of 100,000 random strings with lengths ranging from 1 to 2000 characters. **Comparison Methods** The four comparison methods are: 1. **Regular Expressions (Regex)**: The `regex` variable is set to a regular expression pattern (`/^test$/`) that matches the string "test" at the beginning and end. 2. **startsWith**: This method checks if the string starts with a specified substring ("test"). 3. **indexOf**: This method finds the index of the first occurrence of a specified substring ("test") in the string. 4. **substr**: This method extracts a substring from the original string, starting at the beginning and having a length equal to the length of the matched substring. **Pros and Cons** Here are some pros and cons of each comparison method: * **Regular Expressions (Regex)**: + Pros: Flexible pattern matching, can handle complex patterns. + Cons: Can be slower due to the overhead of compiling and executing regex patterns. * **startsWith**: + Pros: Simple and fast, suitable for most use cases. + Cons: May not work correctly if the string is empty or null. * **indexOf**: + Pros: Fast and efficient, suitable for large strings. + Cons: May perform unnecessary searches if the substring is not found. * **substr**: + Pros: Simple and fast, suitable for most use cases. + Cons: May not be as flexible as regex or `startsWith`. **Library Usage** The benchmark uses a custom JavaScript library that defines the following variables: * `window.regexStart`: A regular expression pattern (`/^test$/`). * `window.regexEnd`: The same regular expression pattern as above (not used in this benchmark). * `window.match`: The string "test" (used for `startsWith`, `indexOf`, and `substr` comparisons). **Special JavaScript Features** This benchmark does not use any special JavaScript features, such as ES6 classes, modules, or async/await. **Alternative Methods** There are alternative comparison methods that could be used in this benchmark: * **String.prototype.includes()**: A method that checks if a string includes a specified substring. * **String.prototype.search()**: A method that finds the index of the first occurrence of a specified substring in the string. * **String.prototype.substring()**: A method that extracts a substring from the original string. These alternative methods could be used to compare performance with the existing comparison methods. However, they may not offer significant benefits or drawbacks depending on the specific use case and requirements.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf simpler
JS Regex vs .startsWith vs .indexOf Jan
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?