Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
number to string: template literal vs toString vs string literal concat vs string constructor
(version: 0)
Comparing performance of:
toString vs template literal vs string literal concat vs String()
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const i = 12345678
Tests:
toString
i.toString()
template literal
`${i}`
string literal concat
"" + i;
String()
String(i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
toString
template literal
string literal concat
String()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
toString
67659368.0 Ops/sec
template literal
90569592.0 Ops/sec
string literal concat
73495176.0 Ops/sec
String()
63810972.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Definition** The benchmark is focused on comparing four different approaches to convert an integer `i` to a string in JavaScript: 1. `toString()` 2. Template literal (``${i}``) 3. String literal concatenation (`"\" + i;`) 4. Using the `String()` constructor (`String(i)`) **Options Compared** The benchmark compares the execution performance of each approach, specifically focusing on the number of executions per second. **Pros and Cons of Each Approach** 1. **`toString()`**: This is a built-in method that converts an object to a string. It's concise and easy to use, but may not be as performant as other methods. 2. **Template Literal (`${i}`)**: Template literals are a relatively new feature in JavaScript that allows you to embed expressions inside string literals. They can provide good performance when used with numeric values. However, they require support for modern browsers and may not work in older environments. 3. **String literal concatenation (`"\" + i;`)**: This method involves concatenating strings using the `+` operator. While it's straightforward to use, it can lead to slower execution times due to string creation overhead. 4. **Using the `String()` constructor (`String(i)`)**: This approach uses a dedicated function to convert an object to a string. It's concise but may not be as performant as other methods. **Library and Special Features** There are no libraries used in this benchmark, and no special JavaScript features or syntax are introduced (other than template literals, which is a relatively new feature). **Other Considerations** * The `Script Preparation Code` sets the value of `i` to 12345678 before running each test case. This ensures that each test case starts with the same input value. * The `Html Preparation Code` is empty in this benchmark, suggesting that no additional setup or configuration is required for the tests. **Alternatives** If you're looking for alternative approaches to convert an integer to a string, you might consider using: 1. **Arrow functions**: `i => i.toString()` 2. **Regular expressions**: `i.toString().replace(/^./g, '')` However, these alternatives are not included in this benchmark and may have different performance characteristics. Overall, the benchmark provides a clear comparison of four common approaches to converting integers to strings in JavaScript, helping users understand which approach is most performant for their specific use cases.
Related benchmarks:
toString vs string template literal
toString vs string template literal vs String()
String() primitive vs template literal interpolation for representing a number as a string
number to string: template literal vs toString vs string literal concat vs string constructor(2)
Comments
Confirm delete:
Do you really want to delete benchmark?