Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JavaScript String Starts/Ends With String Comparisons - Playground
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser:
Chrome 122
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
indexOf at end of sentence v1
4011.2 Ops/sec
indexOf at end of sentence v2
4009.2 Ops/sec
string endsWith
119.3 Ops/sec
HTML Preparation code:
<div></div>
Script Preparation code:
const iterationNum = 250000; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; const possibleLen = possible.length; function getRandomIntInclusive(max, min = 1) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive } function makeRandomString(len, defaultWord = '') { let text = defaultWord; for(let i = 0; i < len; i++) { text += possible.charAt(getRandomIntInclusive(possibleLen, 0)); } return text + defaultWord; } const testWord = window.testWord = makeRandomString(getRandomIntInclusive(10)); const allStrings = window.allStrings = new Array(iterationNum); const numberOfStrings = window.numberOfStrings = iterationNum; for(let i = 0; i < numberOfStrings; i++) { allStrings[i] = makeRandomString(getRandomIntInclusive(20), testWord); }
Tests:
indexOf at end of sentence v1
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordLen = testWord.length; while (x < numberOfStrings) { const str = allStrings[x]; str.indexOf(testWord) === str.length - testWordLen; x += 1; }
indexOf at end of sentence v2
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordLen = testWord.length; while (x < numberOfStrings) { const str = allStrings[x]; str.length - str.indexOf(testWord) !== str.length + 1; x += 1; }
string endsWith
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; while (x < numberOfStrings) { const str = allStrings[x]; str.endsWith(testWord); x += 1; }
Regex match ends with
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordRegExp = new RegExp(`${testWord}$`); while (x < numberOfStrings) { const str = allStrings[x]; str.match(testWordRegExp); x += 1; }
indexOf starts with
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordLen = testWord.length; while (x < numberOfStrings) { const str = allStrings[x]; str.indexOf(testWord) === 0; x += 1; }
search starts with
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordLen = testWord.length; while (x < numberOfStrings) { const str = allStrings[x]; str.search(testWord) === 0; x += 1; }
string startsWith
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; while (x < numberOfStrings) { const str = allStrings[x]; str.startsWith(testWord); x += 1; }
Regex match starts with
let x = 0; const numberOfStrings = window.numberOfStrings; const allStrings = window.allStrings; const testWord = window.testWord; const testWordRegExp = new RegExp(`^${testWord}`); while (x < numberOfStrings) { const str = allStrings[x]; str.match(testWordRegExp); x += 1; }