var a = "some text";
var b = "other text";
a + " aaa " + Math.random() + " bbb" + b + " 3210 " + Math.random() + " ccc";
"".concat(a, " aaa ", Math.random(), " bbb ", b, " 3210 ", Math.random(), " ccc");
`${a} aaa ${Math.random()} bbb ${b} 3210 ${Math.random()} ccc`;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
plus operator | |
concat function | |
template literals |
Test name | Executions per second |
---|---|
plus operator | 625728.4 Ops/sec |
concat function | 630954.7 Ops/sec |
template literals | 629805.5 Ops/sec |
What is being tested?
The provided JSON represents a JavaScript microbenchmark on the MeasureThat.net website. The benchmark tests three different approaches for concatenating strings:
+
operator (plus operator)concat()
function${...}
)Options comparison:
Here's a brief overview of each approach and their pros and cons:
concat()
):${...}
):concat()
function due to string creation and parsing.Library usage:
There is no explicit library mentioned in the benchmark definition. However, it's worth noting that some implementations of template literals may rely on underlying libraries or frameworks for their functionality.
Special JS feature or syntax:
The benchmark uses JavaScript features specifically designed for performance optimization:
Math.random()
is used to add noise and prevent caching.var
declarations for variable assignment, which can help avoid issues with function scope in older browsers.Other alternatives:
In the context of string concatenation, other approaches might include:
String.prototype.repeat()
or template literals with repeated expressions.Keep in mind that the choice of approach depends on the specific use case and performance requirements.