Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf 2
(version: 1)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
startsWith vs indexOf start vs lastIndexOf
Created:
one year ago
by:
Guest
Go to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 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(2000))); }
Tests:
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); 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; }
lastIndexOf
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.lastIndexOf(match, 0); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
indexOf start
lastIndexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
indexOf start
7K/s
startsWith
803/s
lastIndexOf
233/s
View exact numbers
Test name
Executions per second
✓
indexOf start
7,218 Ops/sec
startsWith
803 Ops/sec
lastIndexOf
233 Ops/sec
Related benchmarks
JS Regex vs .startsWith vs .indexOf vs endsWith vs charAt
JS Regex vs .startsWith vs .indexOf vs endsWith vs charAt vs substring
JS Regex vs .startsWith vs .indexOf vs. slice equals vs substring
JS Regex vs .startsWith vs .indexOf and lastIndexOf
JS Regex vs .startsWith vs .indexOf long
Comments
Confirm delete:
Do you really want to delete benchmark?