var name = "name";
for (let i = 0; i < 10; ++i) {
let result = name + ": someItem";
}
for (let i = 0; i < 10; ++i) {
let result = "".concat(name, ": someItem");
}
for (let i = 0; i < 10; ++i) {
let result = `${name}: someItem`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
using plus operator | |
using concat function | |
using template literals |
Test name | Executions per second |
---|---|
using plus operator | 1954964.9 Ops/sec |
using concat function | 1740529.0 Ops/sec |
using template literals | 1921074.9 Ops/sec |
What is being tested?
The provided benchmark tests the performance of three different approaches for concatenating strings in JavaScript:
+
operator (also known as the "string concatenation" or "plus operator")concat()
function${}
)Options comparison:
Here's a brief overview of each approach, their pros and cons, and some considerations:
+
operator:concat()
function:+
operator, as it explicitly separates concatenation from other operations.+
operator.concat()
function is a built-in method that creates a new string object each time it's called. This can lead to unnecessary allocations and copying.${}
):concat()
or the +
operator.Libraries used in test cases:
None of the provided benchmark definitions include any external libraries. However, it's worth noting that some JavaScript engines may use internal libraries for string manipulation.
Special JS features or syntax:
The benchmark uses ES6+ syntax (template literals) and assumes support for let
and const
declarations, which were introduced in ECMAScript 2015.