Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs Concatenation vs Template string
(version: 0)
Comparing performance of:
String() vs .toString() vs Concatenation vs Template string
Created:
4 years 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()); }
Concatenation
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(''+num); }
Template string
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()
Concatenation
Template string
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
498177.4 Ops/sec
.toString()
973399.9 Ops/sec
Concatenation
3251532.8 Ops/sec
Template string
3309127.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares four different methods for converting an integer to a string: 1. `String()` 2. `.toString()` 3. Concatenation (`'' + num`) 4. Template strings (`${num}`) These methods are used in a loop to generate 100 elements, and the execution speed is measured. **Library Usage** None of the benchmark definitions explicitly use any external libraries. However, it's worth noting that `String()` and `.toString()` are built-in JavaScript methods, while concatenation and template strings may work differently across browsers or versions. **Special JS Features/Syntax** Template strings (`${num}`) were introduced in ECMAScript 2015 (ES6). They allow for a more readable way of interpolating variables into string templates. The `'' + num` approach is an older way of concatenating strings using the "+" operator. **Benchmark Results** The benchmark results show that: * Template strings (`${num}`) perform the fastest, with an average execution speed of 2374346 executions per second. * Concatenation (`'' + num`) comes in second, with an average execution speed of 2358853.75 executions per second. * `.toString()` performs slower than concatenation, with an average execution speed of 803412.5625 executions per second. * `String()` performs the slowest, with an average execution speed of 66786.21875 executions per second. **Pros/Cons of Each Approach** Here's a brief summary: 1. **Template strings (`${num}`)**: * Pros: Readable, expressive, and efficient. * Cons: Requires ECMAScript 2015 (ES6) support. 2. **Concatenation (`'' + num`)**: * Pros: Widely supported across browsers and versions. * Cons: Less readable and less efficient than template strings. 3. `.toString()`**: * Pros: Built-in method, widely supported. * Cons: Less efficient than concatenation or template strings. 4. `String()`**: * Pros: None. * Cons: Slowest among the four options. **Alternatives** Other alternatives for converting integers to strings include: 1. Using `parseInt(num.toString())` or `parseFloat(num.toString())`, which can lead to performance issues if not done correctly. 2. Using a library like Lodash's `toString()` method, but this would add external dependency and is generally unnecessary. In conclusion, template strings (`${num}`) are the most efficient way of converting integers to strings in JavaScript, followed by concatenation (`'' + num`). The `.toString()` and `String()` methods perform significantly slower.
Related benchmarks:
Template strings vs. String.concat
Template strings vs. String.concat v2
Template strings vs. String.concat, v2
number to string: template literal vs toString vs string literal concat vs string constructor
Concatenation vs Template String
Comments
Confirm delete:
Do you really want to delete benchmark?