var str = "";
for (var i = 0; i < 1000; ++i) {
str += "String concatenation. ";
}
return str;
var sArr = new Array(1000);
for (var i = 0; i < 1000; ++i) {
sArr[i] = "String concatenation. ";
}
return sArr.join("");
var sArr = [];
for (var i = 0; i < 1000; ++i) {
sArr.push("String concatenation. ");
}
return sArr.join("");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String concatentation | |
Array join | |
Array join (w/ push) |
Test name | Executions per second |
---|---|
String concatentation | 167416.3 Ops/sec |
Array join | 31991.0 Ops/sec |
Array join (w/ push) | 27716.7 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The MeasureThat.net benchmark measures the performance of two ways to concatenate strings in JavaScript: string concatenation, array join, and an alternative approach using push
on an empty array. The test is designed to compare the execution speed of these methods.
Options Compared
+=
operator or the concatenation operator (+
).join()
method.push
: This method creates an empty array and uses the push()
method to add individual strings to the end of the array.Pros and Cons of Each Approach
push
Library and Syntax Used
In this benchmark, the following libraries and syntax are used:
join()
: a built-in JavaScript method for joining arrays of strings into a single string.push()
: a method of arrays in JavaScript that adds one or more elements to the end of an array.Special JS Feature
There is no special JavaScript feature or syntax used in this benchmark, as it focuses on comparing simple concatenation methods.
Other Alternatives
If you're interested in exploring other approaches for string concatenation, consider the following:
Keep in mind that these alternatives might not be as straightforward or widely supported as the methods compared in this benchmark.