var luckyNumber = Math.round(Math.random() * 100);
`SomeString: your lucky number for today is: ${luckyNumber}`
'SomeString:' + 'your lucky number for today is: ' + luckyNumber
'SomeString:' + `your lucky number for today is: ${luckyNumber}`
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
string-interpolation | |
string-concatenation | |
string-concatenation-with-interpolation |
Test name | Executions per second |
---|---|
string-interpolation | 626090752.0 Ops/sec |
string-concatenation | 2455544320.0 Ops/sec |
string-concatenation-with-interpolation | 625136768.0 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark on the website MeasureThat.net. The benchmark compares three different approaches for string interpolation in JavaScript: concatenation, and concatenation with interpolation.
Test Case Breakdown
There are three test cases:
${luckyNumber}
to insert the value of luckyNumber
into a string.+
operator to join strings and insert the value of luckyNumber
.${...}
and +
.Options Compared
The benchmark compares the performance of three different approaches:
${...}
): This approach uses template literals or string interpolation to insert values into a string.+
operator): This approach uses the traditional concatenation method, where strings are joined using the +
operator.Pros and Cons
Here's a brief analysis of each approach:
${...}
):+
operator):Library and Special JS Feature
There is no explicit library mentioned in the benchmark. However, template literals are a built-in JavaScript feature introduced in ECMAScript 2015 (ES6). The interpolation syntax ${...}
relies on this feature.
Other Alternatives
If you want to explore alternative string interpolation methods or concatenation approaches, consider:
join()
or Array.prototype.reduce()
.