Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex with OR (url) vs .startsWith
(version: 1)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
regexStart vs startsWith
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^https:\/\/|^http:\/\//; window.match = 'http'; window.matchLength = window.match.length; var data = window.data = []; const possible = "https://google.com"; 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:
regexStart
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
regexStart
startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regexStart
367.1 Ops/sec
startsWith
1003.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested in the benchmark. **Benchmark Definition** The benchmark compares two approaches for testing whether a string starts with a specific prefix: 1. `regexStart`: This approach uses a regular expression to test if the string matches the prefix. 2. `startsWith`: This approach uses the built-in `startsWith()` method to test if the string starts with the prefix. **Options Compared** Both approaches are compared in terms of their execution speed and accuracy on a set of 100,000 random strings. **Pros and Cons of Each Approach** 1. **regexStart** * Pros: + Can be more flexible, as it allows for regular expression patterns. + May perform better on non-standard prefix formats. * Cons: + Can be slower due to the overhead of compiling and executing a regular expression. + May have higher accuracy requirements, such as specifying flags or modifiers. 2. **startsWith** * Pros: + Typically faster execution speed due to the built-in method's optimization. + More accurate when using the standard `startsWith()` method behavior. * Cons: + Less flexible than regular expressions and may not perform well on non-standard prefix formats. **Library Used** Neither approach explicitly uses a library, but both rely on browser-specific features: 1. **regexStart**: Uses the browser's built-in regular expression engine to compile and execute the regex pattern. 2. **startsWith**: Utilizes the `startsWith()` method, which is a standard JavaScript method implemented by most browsers. **Special JS Features or Syntax** Neither approach uses any special JavaScript features or syntax that would require additional explanation. **Alternatives** If you were to recreate this benchmark, consider using other approaches, such as: 1. **Using a third-party library**: Leverage existing libraries like `regex-approximation` or `string-poseidon` for regular expression optimization and performance. 2. **Custom implementation**: Implement the prefix matching logic from scratch using loops and bitwise operations to compare strings. 3. **Other string comparison methods**: Experiment with other string comparison methods, such as `includes()`, `indexOf()`, or `slice()`. Keep in mind that these alternatives would require significant changes to the benchmark's setup and implementation. Overall, this benchmark provides a good starting point for exploring the trade-offs between regular expressions and built-in string methods like `startsWith()`.
Related benchmarks:
regex vs endWith
JS Regex vs .startsWith vs .indexOf simpler
JS Regex vs .startsWith vs .indexOf Jan
JavaScript: Regex vs .startsWith vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?