for (let i = 0; i < 80; ++i) {
let result = `Some string $i`;
}
for (let i = 0; i < 80; ++i) {
let result = "Some string";
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Template Literal String | |
Regular String |
Test name | Executions per second |
---|---|
Template Literal String | 43448872.0 Ops/sec |
Regular String | 43785784.0 Ops/sec |
In this benchmark defined on MeasureThat.net, we are comparing the performance of two different approaches to string construction in JavaScript: using template literals and regular strings. Both methods are commonly used for creating and manipulating strings, but they differ in syntax and performance characteristics.
Template Literal String:
for (let i = 0; i < 80; ++i) {
let result = `Some string $i`;
}
`
) and allows the inclusion of expressions (in this case, the variable i
) directly within the string.Regular String:
for (let i = 0; i < 80; ++i) {
let result = "Some string";
}
"
). Here, the variable i
is not included in the string, which means the string is static and the expression isn't evaluated.Pros:
Cons:
Pros:
Cons:
From the benchmark results, we can see the following execution rates:
This indicates that creating strings with regular string syntax is slightly faster than using template literals, though both approaches are relatively performant.
+
operator was a common approach. Example: "Some string " + i
. This method can become cumbersome and error-prone with complex strings.Overall, both template literals and regular strings have their place in JavaScript development. The decision on which to use should be based on the specific requirements of the code, the emphasis on performance versus readability, and the familiarity of the team with the modern JavaScript syntax.