Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
number to string: String() vs toString() vs template
(version: 0)
Comparing performance of:
.toString() vs template string vs concat
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
.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}`); }
concat
let nums = []; for(let i = 0; i < 100; ++i) { nums.push("".concat(i)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
.toString()
template string
concat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.toString()
1832427.2 Ops/sec
template string
2802571.8 Ops/sec
concat
1242316.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark. **Benchmark Overview** The `number to string` benchmark is designed to compare the performance of three different approaches for converting numbers to strings: using the `.toString()` method, template literals (template), and concatenation with an empty string (`" "`). **Test Cases** There are three test cases: 1. **`.toString()`**: Converts a number to a string using the `.toString()` method. 2. **Template String**: Uses template literals (`${i}`) to convert a number to a string. 3. **Concatenation**: Concatenates an empty string (`" "`). `concat(i)` is used to convert a number to a string. **Library and Purpose** The `template` approach uses JavaScript's template literal feature, introduced in ECMAScript 2015 (ES6). Template literals allow you to embed expressions inside backticks (`) and create a new string with the values of those expressions. **Special JS Feature/Syntax** The template approach utilizes a special syntax called template literals, which is not widely supported by older browsers. This means that users may need to use a transpiler like Babel or a newer browser to run this benchmark. **Comparison of Options** Here's a brief overview of the pros and cons of each approach: * `.toString()`: + Pros: Wide support across older browsers, no special syntax required. + Cons: Can be slower due to method call overhead. * Template String (`${i}`): + Pros: Faster execution time due to fewer method calls, more readable code. + Cons: Requires newer JavaScript features and/or a transpiler for compatibility with older browsers. * Concatenation: + Pros: Simple and easy to understand. + Cons: Slowest due to repeated concatenations. **Other Alternatives** Other alternatives for converting numbers to strings might include: * Using `String()` (e.g., `String(i)`): This is similar to `.toString()`, but with a function call overhead. * Using `Number.toString.call(i)`: This method uses the `call()` method to invoke the `toString()` method on the `i` object. **Benchmark Results** The latest benchmark results show that: 1. Template String execution time: 783443.9375 executions per second 2. `.toString()` execution time: 447253.03125 executions per second 3. Concatenation execution time: 346192.4375 executions per second These results indicate that the template string approach is the fastest, followed by `.toString()`, and then concatenation. **Conclusion** In conclusion, this benchmark compares the performance of three approaches for converting numbers to strings in JavaScript: `.toString()`, template literals, and concatenation. The template literal approach is faster due to fewer method calls, but it requires newer JavaScript features or a transpiler for compatibility with older browsers.
Related benchmarks:
Casting Number to String
toString vs string template literal
toString vs string template literal vs String()
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
Comments
Confirm delete:
Do you really want to delete benchmark?