window.input = 'This is an aexample of a templated literal supported in modern JavaScript: `test ${test}`.'
window.stringifyByConcatWithSwitch = str => {
let res = '';
for (let i = 0, l = str.length; i < l; ++i) {
let chr = str.charAt(i);
switch (chr) {
case '\\': chr = '\\\\'; break
case '`': chr = '\\`'; break
case '$': chr = '\\$'; break
}
res += chr
}
return `\`${res}\``
}
window.stringifyByRegex = str => str.replace(/\\|`|\$(?={)/g, match => `\\${match}`)
window.stringifyByJoin = str => {
let res = [];
for (let i = 0, l = str.length; i < l; ++i) {
let chr = str.charAt(i)
switch (chr) {
case '\\': chr = '\\\\'; break
case '`': chr = '\\`'; break
case '$': chr = '\\$'; break
}
res.push(chr)
}
return `\`${res.join()}\``
}
const escaped = {
'\\': '\\\\',
'`': '\\`',
'$': '\\$'
}
window.stringifyByConcatWithObject = str => {
let res = ''
for (let i = 0, l = str.length; i < l; ++i) {
let chr = str.charAt(i)
res += escaped[chr] || chr
}
return `\`${res}\``
}
window.stringifyByConcatWithIf = str => {
let res = '';
for (let i = 0, l = str.length; i < l; ++i) {
let chr = str.charAt(i)
if (chr === '\\') chr = '\\\\'
else if (chr === '`') chr = '\\`'
else if (chr === '$') chr = '\\$'
res += chr
}
return `\`${res}\``
}
stringifyByConcatWithSwitch(input)
stringifyByRegex(input)
stringifyByJoin(input)
stringifyByConcatWithObject(input)
stringifyByConcatWithIf(input)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
concatenating with switch | |
regex replacement | |
array joining | |
concatenating with object | |
concatenating with if |
Test name | Executions per second |
---|---|
concatenating with switch | 699959.5 Ops/sec |
regex replacement | 1030719.0 Ops/sec |
array joining | 526777.3 Ops/sec |
concatenating with object | 199868.7 Ops/sec |
concatenating with if | 479213.2 Ops/sec |
Benchmark Description
The benchmark measures the performance of different approaches to convert an input string into a JSON-escaped string, which is essentially a string where special characters are replaced with their corresponding escape sequences.
Test Cases
There are five test cases, each representing a different approach:
switch
statement to determine if each character needs to be escaped. If it does, the corresponding escape sequence is concatenated.switch
statement, this method uses an array to store the escaped characters and then joins them into a single string.switch
statement, this method uses an if-else
chain to determine which escape sequence to use for each special character.Library and Special JS Features
No external libraries are used in these test cases.
However, two special JavaScript features are used:
\
test ${test}`) which allows for simple interpolation of variables into the string.Pros/Cons and Alternatives
Here's a brief summary of the pros and cons of each approach:
switch
overhead.Other alternatives might include using a library like lodash or writing your own custom string escaping function.
Performance Results
The benchmark results show that the regex replacement approach is generally the fastest, followed closely by concatenating with switch. The other approaches have slower performance due to various overheads (e.g., property access in concatenating with object).
Keep in mind that these results are specific to this particular test case and might not reflect real-world usage or other scenarios.