var str = "";
var i;
var sArr = [];
for (i = 1000; i > 0; i--) {
str += "String concatenation. ";
}
for (i = 1000; i > 0; i--) {
sArr[i] = "String concatenation. ";
}
str = sArr.join("");
for (i = 1000; i > 0; i--) {
sArr.push("String concatenation. ");
}
str = 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 | 3543.2 Ops/sec |
Array join | 11442.0 Ops/sec |
Array join (w/ push) | 109.8 Ops/sec |
Benchmark Overview
The benchmark, as defined in the provided JSON, tests three different approaches for concatenating strings: using string concatenation (str +=
), array join (sArr.join()
), and array push with subsequent join (sArr.push()
).
Comparison of Approaches
+=
operator.join()
method.Library Used: None
The benchmark does not use any external libraries or dependencies other than JavaScript's built-in Array
and String
objects.
Special JS Features/Syntax: None
There are no special JavaScript features or syntax used in this benchmark that would require specific knowledge of the language to understand.
Other Alternatives
Alternative approaches to string concatenation could include:
StringBuilder
class, if available in the JavaScript engine being tested.${}
), which can provide a more efficient way to concatenate strings than traditional string concatenation.However, these alternatives may not be supported by all JavaScript engines or platforms being tested in the benchmark.