Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs template string
(version: 0)
Comparing performance of:
String() vs .toString() vs template string
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
String()
let nums = []; for(let i = 0; i < 100; ++i) { nums.push(String(i)); }
.toString()
let nums = []; for(let i = 0; i < 100; ++i) { nums.push(i.toString()); }
template string
let nums = []; for(let i = 0; i < 100; ++i) { nums.push(`${i}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String()
.toString()
template string
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
1005946.8 Ops/sec
.toString()
1914090.8 Ops/sec
template string
3232214.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. The test case uses JavaScript, which is a dynamically-typed language. The benchmark is comparing three different ways to convert numbers to strings: 1. **String()**: This method converts the number to a string using the built-in `String()` function. 2. **.toString()**: This method converts the number to a string using the `toString()` method, which is a part of the object prototype chain for numeric values (e.g., `Number` objects). 3. **Template String**: This method uses template literals (`${}`) to convert the number to a string. Now, let's discuss the pros and cons of each approach: **String()** Pros: Simple and straightforward. Cons: May involve additional overhead due to the dynamic type system, which can lead to slower performance compared to other methods. **.toString()** Pros: Efficient, as it uses an optimized implementation provided by JavaScript engine. Cons: Requires explicit conversion to a `Number` object, which might be unnecessary in some cases. **Template String** Pros: Convenient and expressive, allowing for more flexibility in formatting strings. Cons: May introduce additional overhead due to the parsing process involved in template literals. In terms of performance, the `.toString()` method is likely to be the fastest, as it leverages an optimized implementation provided by the JavaScript engine. However, the difference between `String()` and `.toString()` might be negligible unless you're dealing with extremely large numbers or performance-critical code. Template strings offer a nice trade-off between convenience and performance. While they may introduce some overhead, their concise syntax can make them easier to read and maintain, especially for simple string formatting tasks. To give you a better idea of the results in this specific benchmark, let's look at the execution rates: * Template String: 2557035.5 executions per second * .toString(): 1558067.875 executions per second * String(): 141062.375 executions per second As expected, the template string outperforms both `.toString()` and `String()`, likely due to its optimized parsing process. If you're interested in exploring other alternatives, here are a few: 1. **`Number.toString()`**: Similar to `.toString()`, but uses the `toString()` method of the `Number` class instead. 2. **`+` operator**: Using the unary `+` operator to convert numbers to strings can be another viable option. However, it may have different performance characteristics depending on the JavaScript engine and environment. Keep in mind that these alternatives might not offer significant advantages over the original three methods, but they can provide additional options for experimentation or specific use cases.
Related benchmarks:
Template strings vs. String.concat v2
string vs template vs toString
number to string: template literal vs toString vs string literal concat vs string constructor
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?