Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
regex start vs startsWith vs regex end vs endsWith
Created:
2 years ago
by:
Guest
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(getRandomInt(20))); }
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; }
regex end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexEnd; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
endsWith
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 (4)
Previous results
Fork
Test case name
Result
regex start
startsWith
regex end
endsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex start
740.8 Ops/sec
startsWith
1390.9 Ops/sec
regex end
605.3 Ops/sec
endsWith
1531.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The benchmark measures the performance of four different string comparison methods: `regex.start`, `.startsWith()`, `.endsWith()`, and no comparison (i.e., comparing `0` to `str`). The test data consists of 100,000 randomly generated strings. **Options Compared** 1. **`regex.start`**: Uses a regular expression with the `/^` anchor to match only the beginning of the string. 2. **`.startsWith()`**: A built-in method that checks if the string starts with a given value (in this case, the `match` property). 3. **`.endsWith()`**: A built-in method that checks if the string ends with a given value (in this case, the `match` property). 4. **No comparison** (i.e., comparing `0` to `str`). This test is included to measure the overhead of any non-zero comparisons. **Pros and Cons** 1. **`regex.start`**: * Pros: Can be used to match only the beginning of the string, can be more efficient for larger strings. * Cons: Requires regular expression syntax, may be slower due to compilation time. 2. **`.startsWith()`**: * Pros: Built-in method, efficient for most cases, easy to read and maintain. * Cons: May not be optimized for performance in some browsers or versions. 3. **`.endsWith()`**: * Pros: Built-in method, efficient for most cases, easy to read and maintain. * Cons: Similar to `.startsWith()`, may not be optimized for performance in some browsers or versions. 4. **No comparison**: * Pros: Provides a baseline for any non-zero comparisons, can help isolate comparison overhead. * Cons: Not relevant for actual string comparisons, only serves as a control case. **Library and Purpose** The `window.regexStart` and `window.regexEnd` variables are used to create regular expressions that match the beginning and end of strings, respectively. The purpose of these variables is to provide a consistent and efficient way to perform substring matching in the test data. No specific libraries are used beyond what's built into JavaScript (e.g., `.startsWith()`, `.endsWith()`). **Special JS Feature or Syntax** The benchmark uses no special features or syntax beyond standard JavaScript. **Other Alternatives** If you're interested in exploring alternative string comparison methods, consider: 1. **`.includes()`**: A built-in method that checks if a substring is present in a string. 2. **`String.prototype.localeCompare()`**: A method for comparing strings based on locale-specific rules. 3. **Regular expressions with other anchors** (e.g., `$`, `%`): Can be used to match specific positions or patterns within the string. Keep in mind that each of these alternatives has its own pros and cons, and may not offer significant performance advantages over the methods tested here.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?