First: adding Strings by string
Copy Code code as follows:
var arr = [' Item 1 ', ' Item 2 ', ' Item 3 ', ...];
list = ';
for (var i = 0,
L = arr.length; I < L; i++) {
List + = ' + arr[i] + ';
}
List = ' + list + ';
This is the most common, but the lowest efficiency! Code logic is relatively complex.
The second type: Push into the array one by one
Copy Code code as follows:
var arr = [' Item 1 ', ' Item 2 ', ' Item 3 ', ...],
list = [];
for (var i = 0,
L = arr.length; I < L; i++) {
List[list.length] = ' + arr[i] + ';
}
List = ' + List.join (') + ';
It's a little bit quicker than the last one, but it's not good enough ...
The third type: Direct join ()
Copy Code code as follows:
var arr = [' Item 1 ', ' Item 2 ', ' Item 3 ', ...];
var list = ' + Arr.join (') + ';
Using native methods (such as join ()), regardless of how it is implemented later, is generally much faster than other methods, and the code is very concise.
Browser Performance test
Each method is tested with an array of length 130, each of which has a variety of lengths, preventing browsers from making special optimizations to a certain length of string, 1000 tests per method, and the following results: The time required to perform these 1000 times