Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf (string length 50 - 550)
(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 vs indexOf start vs indexOf end
Created:
4 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(500)+50)); }
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; }
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; }
indexOf end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var length = window.matchLength; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === str.length - length; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
regex start
startsWith
regex end
endsWith
indexOf start
indexOf end
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):
I'll break down the benchmark and its various components. **Benchmark Overview** The MeasureThat.net benchmark is designed to compare the performance of four string matching operations: 1. `regex.start` (also known as "regular expression start") 2. `startsWith` 3. `regex.end` (also known as "regular expression end") 4. `indexOf` with two variants: * `indexOf.start`: starts searching from the beginning of the string * `indexOf.end`: searches for a substring at the end of the original string The benchmark generates a large dataset of random strings and measures the execution time for each operation. **Options Compared** The four options are: 1. **`regex.start`**: uses a regular expression to match the entire string 2. **`startsWith`**: checks if the string starts with a given prefix 3. **`regex.end`**: uses a regular expression to match the entire string, similar to `regex.start`, but without the `$` anchor (which would match at the end of the string) 4. **`indexOf.start`** and **`indexOf.end`**: search for a substring starting from the beginning or ending of the original string **Pros and Cons** Here's a brief summary of each option: 1. `regex.start`: * Pros: can be more efficient than other options when the regular expression is complex or the strings are long * Cons: requires compiling the regular expression, which can add overhead 2. `startsWith`: * Pros: simple and fast, with good performance for small prefixes * Cons: may not work well if the prefix is not present in the string, leading to false negatives 3. `regex.end`: * Pros: similar efficiency to `regex.start`, but without the `$` anchor * Cons: requires compiling the regular expression and using a different anchor than `regex.start` 4. `indexOf.start` and `indexOf.end`: * Pros: can be faster than `startsWith` when searching from the beginning or end of the string * Cons: may not work well if the substring is not present in the original string, leading to false negatives **Benchmark Results** The latest benchmark results show that: * `regex.start` and `regex.end` are relatively close in performance, with `regex.start` being slightly faster on average * `startsWith` is significantly slower than the two regular expression options * `indexOf.start` and `indexOf.end` are generally slower than `startsWith`, but faster than `regex.start` and `regex.end` **Conclusion** In summary, the benchmark highlights the trade-offs between different string matching operations: * Regular expressions (`regex.start` and `regex.end`) offer good performance for complex patterns or long strings, but require compiling the regular expression. * `startsWith` is simple and fast, but may not work well if the prefix is not present in the string. * `indexOf.start` and `indexOf.end` can be faster than `startsWith`, but may not work well if the substring is not present in the original string. The choice of operation depends on the specific use case and requirements.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
Comments
Confirm delete:
Do you really want to delete benchmark?