Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs template literal
(version: 0)
Comparing performance of:
String() vs .toString() vs template literal vs gato
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
String()
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(String(num)); }
.toString()
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(num.toString()); }
template literal
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(`${num}`); }
gato
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push('' + num); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
String()
.toString()
template literal
gato
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
3509922.0 Ops/sec
.toString()
3527494.2 Ops/sec
template literal
1042908.8 Ops/sec
gato
3500685.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're analyzing compares various approaches to convert a number to a string in JavaScript. Specifically, it evaluates the performance of four different methods: 1. **`String()` Constructor** 2. **`.toString()` Method** 3. **Template Literals** 4. **Concatenation with Empty String** ### Options Compared 1. **`String(num)`**: - This approach utilizes the global `String` constructor to convert the number. - **Pros**: Simple and straightforward to use. - **Cons**: It is the slowest among the tested methods, as indicated by the benchmark results, potentially due to function call overhead. 2. **`num.toString()`**: - This method invokes the `toString` method of the number object. - **Pros**: More efficient than using `String()`, as it's directly tied to the number instance. - **Cons**: Slightly less readable than `String()` for someone unfamiliar with methods in JavaScript, and can fail for `null` or `undefined`, throwing an error. 3. **Template Literals (``${num}``)**: - Introduced in ES6 (ECMAScript 2015), this syntax allows embedding expressions within a string by wrapping them in backticks. - **Pros**: Highly readable and maintains clear intent. It has shown to be the fastest in the benchmark, indicating it may be optimized well by JavaScript engines. - **Cons**: Slightly less direct than using the `String` constructor or `toString()`, but this concern is mitigated by its performance and readability. 4. **Concatenation with an Empty String (`'' + num`)**: - This method converts a number to a string by concatenating an empty string. - **Pros**: Simple and often performs well. It effectively leverages JavaScript’s implicit type conversion. - **Cons**: Can be less clear to someone reading the code for the first time, as it relies on JavaScript's type coercion rules. ### Benchmark Results Overview The benchmark results show the number of executions per second for each method in the Chrome 130 environment on a Windows platform: - **Template Literals**: 4,881,105 executions/sec (fastest) - **Concatenation with an Empty String**: 4,803,526 executions/sec (second fastest) - **`.toString()`**: 1,372,328 executions/sec - **`String()` Constructor**: 102,193 executions/sec (slowest) ### Other Considerations - **Readability vs. Performance**: While performance is essential, code maintainability and readability should also be considered. `String(num)` and `.toString()` may be more familiar to developers, while template literals and concatenation may require some initial learning. - **Context of Use**: The choice may depend on the context in which the conversion is used. For instance, if a number conversion is done frequently in a hot code path, the performance difference would become more relevant. - **Browser-specific Performance**: The performance results could vary across different browsers and JavaScript engines, so testing in the target environment is advisable. ### Alternatives Not Tested - **Third-party Libraries**: Libraries like Lodash provide utility methods that can also transform data types but are generally more oriented toward complex operations than simple type conversions. - **Manual String Manipulation**: Custom implementations may yield performance insights but generally wouldn’t outperform native methods and would sacrifice readability. In conclusion, for most use cases, template literals or concatenation are optimal choices for converting numbers to strings in JavaScript, balancing performance and readability effectively.
Related benchmarks:
String() vs .toString() vs template string
String() vs .toString() vs template string vs concat with string
String() vs .toString() vs Concatenation vs Template string
String() vs .toString() vs template string (optim)
String() vs .toString() vs template string vs. short transform
String() vs .toString() vs concat vs template
String() vs .toString() vs template string vs add to string
String() vs .toString() vs template string vs add to string 2
number to string: String() vs toString() vs template
Comments
Confirm delete:
Do you really want to delete benchmark?