Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf vs .includes
(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 includes
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(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; }
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; }
includes
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.includes(match); 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
includes
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 dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents a benchmark test comparing the performance of four different string matching methods in JavaScript: 1. `String.prototype.indexOf()`: searches for a specified value (in this case, the match) from the start of a string. 2. `String.prototype.includes()`: searches for a specified value (in this case, the match) anywhere in a string. 3. `RegExp.prototype.test()`: tests whether a regular expression matches a specified value (in this case, the match). 4. `String.prototype.startsWith()` and `String.prototype.endsWith()`: test whether a string starts or ends with a specified value (in this case, the match). **Options compared** The benchmark compares the performance of each method: * `indexOf` is tested on strings starting from different positions. * `includes` is tested on strings of varying lengths. * `test` is used with regular expressions and literal values. * `startsWith` and `endsWith` are tested on strings with prefixes and suffixes. **Why these methods?** The choice of methods likely aims to showcase: 1. `indexOf`: a fundamental string matching method that's widely used in JavaScript. 2. `includes`: a more modern method introduced in ECMAScript 2015, which is gaining popularity for its simplicity and efficiency. 3. `test` with regular expressions: a powerful tool for pattern matching, but potentially slower due to the complexity of regex engines. 4. `startsWith` and `endsWith`: string methods that are often used together, making their performance worth comparing. **Benchmarks results** The latest benchmark results show that: * `includes` performs the best among all methods, followed closely by `indexOf`. * `test` with regular expressions is significantly slower than the other methods. * `startsWith` and `endsWith` are relatively fast, but still outperformed by `includes`. **Device and browser** The test was conducted on a Chrome Mobile 104 device running Android 11. Keep in mind that these results might vary depending on your specific environment, JavaScript engine, and hardware. The benchmark serves as a general guideline for the relative performance of each method.
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 startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?