Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string cast vs template literal cast
(version: 1)
cast to string or template literal wrapping
Comparing performance of:
String literal vs Template literal
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); } const TEST_ITERATIONS=100000;
Tests:
String literal
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/ for (let i=0; i++; i<TEST_ITERATIONS) { const stringLiteral = String(i); }
Template literal
for (let i=0; i++; i<TEST_ITERATIONS) { const templateLiteral = `${i}` }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String literal
Template literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 140 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String literal
45725516.0 Ops/sec
Template literal
48813832.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The provided benchmark compares two methods of converting numbers to strings in JavaScript: using the `String()` function (also referred to as "String literal") and using template literals. The benchmark aims to determine which of these two approaches performs better in terms of execution speed. ### Test Cases Overview 1. **String Literal**: - **Benchmark Definition**: ```javascript for (let i = 0; i < TEST_ITERATIONS; i++) { const stringLiteral = String(i); } ``` - This method uses the `String()` function to convert the integer `i` to a string. The code runs this conversion for a defined number of iterations (100,000 in this scenario). 2. **Template Literal**: - **Benchmark Definition**: ```javascript for (let i = 0; i < TEST_ITERATIONS; i++) { const templateLiteral = `${i}`; } ``` - This method employs JavaScript's template literals to convert the integer `i` to a string. Template literals are enclosed by backticks (`` ` ``) and allow for embedding expressions within the string. Again, the conversion is performed for 100,000 iterations. ### Pros and Cons #### String Literal (`String()` function) - **Pros**: - Clear and straightforward; explicitly indicates the intention to convert to a string. - Can handle a variety of input types (undefined, null, etc.) and convert them to string representations. - **Cons**: - Slightly more overhead compared to simpler operations due to the underlying function call, which might introduce some performance cost in tight loops. #### Template Literal - **Pros**: - Provides a concise syntax to perform the string conversion and can easily be combined with additional text. - Offers enhanced readability, especially when constructing complex strings. - **Cons**: - May seem less explicit regarding the conversion when only simple number-to-string transformation is intended. - The performance may vary based on JavaScript engine optimizations regarding template literal parsing. ### Benchmark Results The results of the benchmark indicated that: - The "String literal" achieved **234,916,336 executions per second**. - The "Template literal" reached **223,732,416 executions per second**. In this particular test, the `String()` function performed slightly better than the template literal approach. ### Other Considerations - **Performance Variability**: It's important to recognize that performance results can vary based on the JavaScript engine, the specific environment, and even the device being used. Different browsers and their versions may implement optimizations differently. - **Other Alternatives**: - **Concatenation**: Another alternative to consider is simply concatenating an empty string with the number (e.g., `const stringConcat = i + "";`). This method is often used for its succinctness and can be similar in performance to the `String()` function in many environments. - **Number.toString()**: Using the `toString` method on a number (e.g., `const numToString = i.toString()`) could also be a viable option for string conversion. ### Conclusion In selecting between these two methods, developers should weigh the performance outcomes against readability and maintainability for their specific applications. While benchmarks like this provide useful insights, the choice may ultimately depend on the context of use and developer preference.
Related benchmarks:
Nullish vs If
JS Variable Performance (const vs let vs var)
js mul vs pow
String literal vs Template literal assignment
Test direct and destructuring performances
test-string-1
classNames
benchmark for benchmarks
Reassignment of Var vs Const vs Let
Comments
Confirm delete:
Do you really want to delete benchmark?