JavaScript has many ways to create arrays in batches. to measure their performance, you can create an array of 100000 characters in length using different methods, and the keys and values are equal. JavaScript has many ways to create arrays in batches. to measure their performance, I use different methods to create an array with a length of 100000 and the keys and values are equal. At the same time, I have defined the following function to measure the time it takes to create an array:
Function t (fn) {var start = Date. now (); fn. call (this); var end = Date. now (); return (end-start) + 'ms ';}
The following are several common methods for creating arrays and their time consumption:
Use join and split
After comparison, it is found that the original for loop has the fastest direct assignment speed, and the other methods have the same speed.
However, it is really troublesome to write for a for loop. It takes three sentences to get it done in one sentence.
Therefore, if there is not much requirement on performance (after all, there will not be an Array as large as 100000 in actual development), it is most convenient to use apply and Array. from.
The preceding figure shows how to use javascript to create arrays in batches. For more information, see other related articles in the first PHP community!