Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
stringify by concatenating or regex replacement or array joining
(version: 1)
Comparing performance of:
concatenating with switch vs regex replacement vs array joining vs concatenating with object vs concatenating with if
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
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}\`` }
Tests:
concatenating with switch
stringifyByConcatWithSwitch(input)
regex replacement
stringifyByRegex(input)
array joining
stringifyByJoin(input)
concatenating with object
stringifyByConcatWithObject(input)
concatenating with if
stringifyByConcatWithIf(input)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
concatenating with switch
regex replacement
array joining
concatenating with object
concatenating with if
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests different ways to escape special characters in a string for use within a template literal. Template literals are enclosed in backticks (``) and allow you to embed expressions directly into strings using the `${}` syntax. Here's a breakdown of the options compared: * **`stringifyByConcatWithSwitch`**: This approach uses a `switch` statement to replace special characters (backslash, backtick, dollar sign) with their escaped equivalents before concatenating them into a string. * **`stringifyByRegex`**: This method employs a regular expression to find the special characters and replaces them with their escaped counterparts. * **`stringifyByJoin`**: This approach uses an array to store each character in the string, replacing special characters as needed, and then joins the elements back into a single string using `\`` around it. * **`stringifyByConcatWithObject`**: This method defines an object mapping special characters to their escaped versions and iterates through the input string, substituting with the escaped values. * **`stringifyByConcatWithIf`**: Similar to the `switch` approach, but uses a series of `if` statements to check for each special character and perform the replacement. **Pros and Cons:** * **`stringifyByConcatWithSwitch` & `stringifyByConcatWithIf`:** * **Pros:** Can be relatively readable if the number of special characters is small. * **Cons:** Can become verbose for a large number of special characters. * **`stringifyByRegex`**: * **Pros:** Can be very efficient, especially for complex pattern matching. * **Cons:** Regular expressions can be harder to read and debug than other methods. * **`stringifyByJoin`**: * **Pros:** Avoids direct string manipulation, potentially offering better performance in some cases. * **Cons:** Might be slightly less readable compared to simpler methods. * **`stringifyByConcatWithObject`**: * **Pros:** Can be concise and efficient if the mapping of characters is well-defined. * **Cons:** Requires upfront creation of the character mapping object. **Alternatives:** For modern JavaScript, using built-in features like template literals' ability to handle expressions within them often simplifies escaping and string manipulation. However, this benchmark likely tests various techniques for educational purposes or in scenarios where direct control over string construction is necessary. Let me know if you have any other questions about the code or the results!
Related benchmarks:
regexReplace vs arrayReplace
Battle of strings
Battle of strings
Uppercase 2
Comments
Confirm delete:
Do you really want to delete benchmark?