Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JavaScript: Regex vs .startsWith vs .indexOf
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser:
Chrome 130
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
startsWith
46.8 Ops/sec
endsWith
46.0 Ops/sec
Regex start
223.4 Ops/sec
Regex end
209.4 Ops/sec
indexOf start
43.6 Ops/sec
indexOf end
23.3 Ops/sec
HTML Preparation code:
<div></div>
Script Preparation code:
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:
startsWith
window.data.forEach(str => str.startsWith(window.match));
endsWith
window.data.forEach(str => str.endsWith(window.match));
Regex start
var regex = /^test/; window.data.forEach(str => regex.test(str));
Regex end
var regex = /test$/; window.data.forEach(str => regex.test(str));
indexOf start
window.data.forEach(str => str.indexOf(window.match) === 0);
indexOf end
window.data.forEach(str => str.indexOf(window.match) === str.length - window.matchLength);