Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf vs contains longer strings
(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 vs contains
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(200))); }
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; }
contains
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.includes(match) === true x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
regex start
startsWith
regex end
endsWith
indexOf start
indexOf end
contains
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 components. **Benchmark Overview** The benchmark compares the performance of four string manipulation methods in JavaScript: 1. `RegExp` (regex) with start anchor (`^`) 2. `String.prototype.startsWith()` 3. `String.prototype.indexOf()` with start index 0 4. `String.prototype.includes()` (contains) These methods are used to search for a specific pattern or substring within a longer string. **Options Compared** The benchmark compares the performance of each method on four different strings, but only shows the results for two of them: "indexOf end" and "startsWith". Here's what we can infer about the other methods: * `RegExp` with start anchor (`^`) is likely the fastest because it uses a compiled regular expression engine that can optimize the search process. * `String.prototype.startsWith()` checks if the string starts with the specified substring, but may be slower than `RegExp` due to the overhead of checking each character in the string. * `String.prototype.indexOf()` searches for the first occurrence of the substring and returns its index. It's likely to be faster than `startsWith()` because it doesn't need to check if the entire string starts with the substring. * `String.prototype.includes()` checks if the string contains the specified substring, which may be slower than `indexOf()` or `RegExp` due to the overhead of checking each character in the string. **Benchmark Results** The latest benchmark results show that: * `indexOf end`: 6424.833984375 executions per second (fastest) * `startsWith`: 467.1417236328125 executions per second * `regex start`: 333.1871337890625 executions per second * `contains`: 147.6510009765625 executions per second **Interpretation** The results suggest that: * `indexOf end` is the fastest method, likely because it uses a simple and efficient search algorithm. * `RegExp` with start anchor (`^`) is not the fastest in this benchmark, possibly due to the overhead of compiling and executing the regular expression. Keep in mind that these results may vary depending on the specific use case and requirements. The best approach will depend on the specific performance needs of your application.
Related benchmarks:
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
JS startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?