Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf for markdown header
(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 indexOf start
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^#+ +(.*)/u; window.match = '# '; 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; }
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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex start
startsWith
indexOf start
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 what is being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of three different approaches for checking if a string matches a certain pattern: 1. Using a regular expression (`regex` variable) 2. Using the `startsWith()` method 3. Using the `indexOf()` method with a start index **Option Comparison** Here's a brief summary of each approach, their pros and cons, and other considerations: ### Regular Expression (`regex` variable) * **Pros:** Highly flexible and powerful pattern matching, can match complex patterns. * **Cons:** Can be slow due to the overhead of compiling regular expressions. * **Other Considerations:** The `regex` variable is set up to match strings that start with one or more `#` characters followed by any character. This pattern is specific to Markdown headers. ### `startsWith()` method * **Pros:** Fast and simple, only checks if the string starts with a certain substring. * **Cons:** Limited flexibility, can only check for exact substrings. * **Other Considerations:** The `startsWith()` method is called on each string in the dataset without any filtering or optimization. ### `indexOf()` method with start index * **Pros:** Similar to `startsWith()`, but allows for more flexible searching (can search from a specific position). * **Cons:** Still relatively slow compared to native JavaScript methods, and can be slower than `startsWith()` due to the overhead of string indexing. * **Other Considerations:** The `indexOf()` method is called on each string in the dataset without any filtering or optimization. **Library and Special Features** The benchmark uses a custom variable `window.regexStart` to define the regular expression pattern for matching Markdown headers. This variable is set up only once before running the benchmarks. There are no special features or syntax used in this benchmark, other than the custom `regex` variable defined above. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Native JavaScript methods:** Instead of using regular expressions or string methods, you could use native JavaScript methods like `String.prototype.startsWith()` and `String.prototype.indexOf()`. 2. **Array.prototype.some() and Array.prototype.every():** These methods can be used to check if any or all strings in an array match a certain condition. 3. **Regular expression APIs:** You could explore using other regular expression APIs, like `RegExp.test()` or `RegExp.exec()`, which may offer better performance for specific use cases. Keep in mind that each approach has its strengths and weaknesses, and the best choice will depend on your specific requirements and performance constraints.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf by Jarvis
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
Comments
Confirm delete:
Do you really want to delete benchmark?