Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string[index] vs charAt(index) vs charCodeAt(index)
(version: 1)
Compare same algorithm using javascript String[index] vs String.charAt(index) vs String.charCodeAt(index)
Comparing performance of:
String[index] vs String.charAt(index) vs String.charCodeAt(index)
Created:
3 years ago
by:
Registered User
Jump to the latest result
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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String[index]
String.charAt(index)
String.charCodeAt(index)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
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/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
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
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Overview** The benchmark compares three different approaches to extract parts from a string: 1. `String[index]` 2. `String.charAt(index)` 3. `String.charCodeAt(index)` These methods are used to parse strings by extracting individual parts, such as values or quoted substrings, using a specific index or character. **Methods Overview** Each method has its own way of parsing the string: * `String[index]`: This method uses bracket notation (`[index]`) to access and extract characters from the string. It returns the value at that index. * `String.charAt(index)`: Similar to `String[index]`, but this method explicitly calls the `charAt()` function to get the character at the specified index. * `String.charCodeAt(index)`: This method uses the ASCII code of a character (i.e., its Unicode code point converted to an integer value between 0 and 127) to extract characters from the string. It returns the Unicode code point at that index. **Test Cases** The benchmark consists of four test cases: 1. `partsCount = 0` followed by `for (const testCase of testCases)` ... Each loop iterates over a subset of test cases and calls the respective parsing method (`String[index]`, `String.charAt(index)`, or `String.charCodeAt(index)`) to extract parts from each test case. 2. The extracted parts are then counted using the variable `partsCount`. 3. After all iterations, if the expected number of parts (16) is not equal to the actual count (`partsCount`), an error message will be thrown. **Results** The benchmark results display data for each of three test cases: * Each row contains information about a specific browser instance, including its raw user agent string, browser version, device platform, operating system, and executions per second. * The `TestName` field corresponds to the respective parsing method being tested. **Benchmark Implications** In summary, this benchmark evaluates three different methods for extracting parts from strings: 1. **Efficiency**: Which method is faster? Does it prioritize execution speed over accuracy or vice versa? 2. **Robustness**: How well do these methods handle edge cases or malformed input data? 3. **Browser Support**: Are the parsing methods universally supported across different browsers and platforms? **Potential Optimization Opportunities** Considering the parsing methods used, potential optimizations for performance enhancements: * Implementing more efficient indexing or substring extraction techniques (e.g., string slicing, regular expressions) might lead to better performance. * Considering caching mechanisms to store precomputed results could speed up repeated computations. Feel free to ask me any specific follow-up questions.
Related benchmarks:
char index vs charAt() vs slice() for the last character
char index vs charAt() vs slice() vs startsWith()
char index vs charAt() vs slice() vs at()
Find index in string with recursion vs findIndex
Comments
Confirm delete:
Do you really want to delete benchmark?