Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser:
Chrome 120
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Benchmark engine:
Benchmark.js
String[index]
665K/s
String.charAt(index)
545K/s
String.charCodeAt(index)
75K/s
View exact numbers
Test name
Executions per second
✓
String[index]
665,090 Ops/sec
String.charAt(index)
544,675 Ops/sec
String.charCodeAt(index)
74,960 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)