Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs RegExp
(version: 0)
Comparing performance of:
regexp vs startsWith
Created:
4 years ago
by:
Guest
Jump to the latest result
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(getRandomInt(20))); }
Tests:
regexp
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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regexp
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's being tested, compared, and the pros and cons of different approaches. **Benchmark Overview** The benchmark compares two approaches for checking if a string starts with a given pattern: using `RegExp` (Regular Expressions) versus using the `startsWith()` method. **Script Preparation Code** The script preparation code sets up some global variables: * `window.regexStart`: a RegExp object that matches any string starting with "test" * `window.regexEnd`: not used in this benchmark, but likely intended to match any string ending with "test" (not relevant to the test) * `window.match`: set to the string "test", which will be used as the pattern * `window.matchLength`: stores the length of the `match` string * `window.data`: an array of 100,000 random strings generated using the `makeRandomString()` function (explained below) * `TOTAL_STRINGS`: sets the total number of strings in the `data` array **makeRandomString() Function** The `makeRandomString(len)` function generates a random string of length `len` by concatenating characters from a predefined alphabet (`possible`) using `Math.random()`. **Individual Test Cases** There are two test cases: 1. **RegExp**: uses the `regexStart.test(str)` expression to check if each string in `data` starts with "test" 2. **startsWith**: uses the `str.startsWith(match) === 0` expression to check if each string in `data` starts with "test" **Comparison** The benchmark compares the performance of these two approaches by executing each test case on a large dataset of random strings (`window.data`). The results are reported for each browser and device platform. **Pros and Cons** * **RegExp (startsWith is not used)**: + Pros: can match more complex patterns using the `RegExp` syntax, provides more control over matching. + Cons: slower than the `startsWith()` method due to the overhead of compiling a RegExp object, may perform unnecessary backtracking or flagging. * **startsWith**: + Pros: faster and more efficient than the `RegExp` approach, optimized for simple prefix matching. + Cons: limited to exact prefix matching, less flexible than RegExp. The use of `startsWith()` is generally considered more suitable when: * The pattern is a fixed string * The pattern is short (a few characters) * Performance is critical On the other hand, `RegExp` might be preferred when: * The pattern is complex or requires regular expression features like quantifiers or anchors * The need for precise control over matching outweighs performance considerations
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
.substr vs .startwith
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
Comments
Confirm delete:
Do you really want to delete benchmark?