let text = [];
for (i = 10000; i > 0; i--) {
text[i] = "some text";
}
const val = text.join(" ");
var text = [];
for (i = 10000; i > 0; i--) {
text.push('some text');
}
var val = text.join(" ");
var text = '';
for (i = 10000; i > 0; i--) {
text += ` some text`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array concat | |
array push | |
concat |
Test name | Executions per second |
---|---|
array concat | 4304.5 Ops/sec |
array push | 8859.6 Ops/sec |
concat | 21466.9 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Overview
The benchmark measures the performance of three different ways to concatenate strings or arrays in JavaScript:
+
operator to concatenate two strings, such as "some text" + "another text"
(later modified to use template literals).push()
method to add an element to an array and then joining the array using the join()
method.${expression}
) to concatenate strings.Library Usage
None of the benchmark tests use any external libraries.
Special JavaScript Features/Syntax
The benchmark uses template literals, which is a relatively modern feature in JavaScript introduced in ECMAScript 2015 (ES6). Template literals allow for more readable and efficient string concatenation using the ${expression}
syntax. However, older JavaScript engines might not support this feature or have limited support.
Options Compared
The three options are compared in terms of their performance:
+
operator to concatenate strings.push()
method to add an element to an array and then joining the array using the join()
method.${expression}
) to concatenate strings.Pros and Cons of Each Approach
Other Considerations
The benchmark also provides information on the browser, device platform, operating system, and execution speed for each test. This can help analyze performance variations across different environments.
If you're interested in exploring alternative approaches or understanding more about template literals, here are some additional resources: