Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf (largerStrings)
(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))); }
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):
Let's break down the benchmark and its various components. **Benchmark Overview** The benchmark measures the performance of four string comparison methods: 1. `String.prototype.startsWith()` 2. `String.prototype.indexOf()` 3. `RegExp.prototype.test()` with a positive lookahead (`^`) 4. `RegExp.prototype.test()` with a negative lookahead (`$`) These comparisons are performed on large strings generated randomly. **Options Compared** The benchmark compares the performance of each string comparison method: 1. `startsWith`: checks if a string starts with a specified prefix 2. `indexOf`: finds the index of the first occurrence of a specified substring within a string 3. `RegExp.test` with positive lookahead (`^`): checks if a string matches a regular expression pattern that requires the first character to be a certain value (in this case, any letter) 4. `RegExp.test` with negative lookahead (`$`): checks if a string matches a regular expression pattern that requires the last character to be a certain value (in this case, any letter) **Pros and Cons** 1. **startsWith**: Simple and efficient, but may not work well for non-English characters or substrings. 2. **indexOf**: Works well with all types of strings and substrings, but may be slower than `startsWith` due to its more complex algorithm. 3. **RegExp.test` with positive lookahead (`^`): Can be slower than `startsWith` because it requires compiling a regular expression and executing it for each string. 4. **RegExp.test** with negative lookahead (`$`): Similar to the previous case, but may have additional overhead due to the reversed logic. **Library Usage** The benchmark uses no external libraries, relying on built-in JavaScript features: 1. `String.prototype.startsWith()` 2. `String.prototype.indexOf()` 3. `RegExp.prototype.test()` (used with and without lookahead) **Device Platform and Browser** The benchmark is run on a Windows desktop with Chrome 94, which provides some insight into the performance of these methods in a specific browser and platform configuration. **Conclusion** In summary, this benchmark measures the performance of four string comparison methods: `startsWith`, `indexOf`, `RegExp.test` with positive lookahead (`^`), and `RegExp.test` with negative lookahead (`$`). The results suggest that `startsWith` is generally the fastest method, followed by `indexOf`, while the regular expression tests are slower due to compilation overhead.
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
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
Comments
Confirm delete:
Do you really want to delete benchmark?