Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
string[index] vs charAt(index) vs charCodeAt(index)
Compare same algorithm using javascript String[index] vs String.charAt(index) vs String.charCodeAt(index)
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/124.0.0.0 Safari/537.36
Browser:
Chrome 124
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
String[index]
486513.8 Ops/sec
String.charAt(index)
375268.0 Ops/sec
String.charCodeAt(index)
58410.3 Ops/sec
Script Preparation code:
const PERIOD = '.'; const SINGLE_QUOTE = "'"; const DOUBLE_QUOTE = '"'; const BACK_TICK = '`'; function getPartsWithStringIndex(text) { const parts = []; let path; let partStart = 0; let partEnd = -1; let quotedWith = null; let previous; let current; for (let index = 0; index <= text.length; index++) { previous = current; current = text[index]; if (index === partStart) { if (current === DOUBLE_QUOTE || current === SINGLE_QUOTE || current === BACK_TICK) { quotedWith = current; continue; } } if (typeof current === 'undefined' || (current === PERIOD && (!quotedWith || previous === quotedWith))) { partEnd = index; } if (partStart < partEnd) { if (quotedWith) { path = text.substring(partStart + 1, index - 1); quotedWith = null; } else { path = text.substring(partStart, index); } parts.push({ path }); partStart = index + 1; } } return parts; } function getPartsWithCharAt(text) { const parts = []; let path; let partStart = 0; let partEnd = -1; let quotedWith = null; let previous; let current; for (let index = 0; index <= text.length; index++) { previous = current; current = text.charAt(index); if (index === partStart) { if (current === DOUBLE_QUOTE || current === SINGLE_QUOTE || current === BACK_TICK) { quotedWith = current; continue; } } if (current === '' || (current === PERIOD && (!quotedWith || previous === quotedWith))) { partEnd = index; } if (partStart < partEnd) { if (quotedWith) { path = text.substring(partStart + 1, index - 1); quotedWith = null; } else { path = text.substring(partStart, index); } parts.push({ path }); partStart = index + 1; } } return parts; } const PERIOD_CODE = '.'.charCodeAt(0); const SINGLE_QUOTE_CODE = "'".charCodeAt(0); const DOUBLE_QUOTE_CODE = '"'.charCodeAt(0); const BACK_TICK_CODE = '`'.charCodeAt(0); function getPartsWithCharCodeAt(text) { const parts = []; let path; let partStart = 0; let partEnd = -1; let quotedWith = null; let previous; let current; for (let index = 0; index <= text.length; index++) { previous = current; current = text.charCodeAt(index); if (index === partStart) { if (current === DOUBLE_QUOTE_CODE || current === SINGLE_QUOTE_CODE || current === BACK_TICK_CODE) { quotedWith = current; continue; } } if (isNaN(current) || (current === PERIOD_CODE && (!quotedWith || previous === quotedWith))) { partEnd = index; } if (partStart < partEnd) { if (quotedWith) { path = text.substring(partStart + 1, index - 1); quotedWith = null; } else { path = text.substring(partStart, index); } parts.push({ path }); partStart = index + 1; } } return parts; } var testCases = [ // subset of cases 'path.to.a.value', 'pathTo.someSpecified.aValue', 'path."with `inner` quoted"."`parts`"', 'path."with spaces"."to a value"', 'path."with.dots"."to.a.value"' ] var partsCount = 0
Tests:
String[index]
partsCount = 0 for (const testCase of testCases) { const parts = getPartsWithStringIndex(testCase) partsCount += parts.length } if (partsCount !== 16) throw new Error('String[index] invalid parts count ' + partsCount)
String.charAt(index)
partsCount = 0 for (const testCase of testCases) { const parts = getPartsWithCharAt(testCase) partsCount += parts.length } if (partsCount !== 16) throw new Error('String[index] invalid parts count ' + partsCount)
String.charCodeAt(index)
partsCount = 0 for (const testCase of testCases) { const parts = getPartsWithCharCodeAt(testCase) partsCount += parts.length } if (partsCount !== 16) throw new Error('String[index] invalid parts count ' + partsCount)