There are many methods to connect html strings, but the efficiency is quite different. You can choose one.
First: Add strings one by one
The Code is 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 ='
';
This is the most common, but the most efficient! The Code logic is relatively complex.
Type 2: push the data to the array one by one
The Code is 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 ='
';
It is a little faster than the previous method, but it is not good enough...
Third: Direct join ()
The Code is as follows:
Var arr = ['item 1', 'item 2', 'item 3',...];
Var list ='
';
The use of native methods (such as join (), no matter how it is implemented later, is generally much faster than other methods, and the code is very concise.
Browser Performance
Each method uses an array with a length of 130 for testing. The length of each element is varied to prevent the browser from making special Optimizations to strings of a certain length; each method is tested for 1000 times. The following results show the time required to perform the 1000 times: