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, today, I will share with you how to create arrays in batches Based on js. Let's take a look at the many methods to create arrays in batches in JavaScript. to measure their performance, I used different methods to create an array with a length of 100000 and the keys and values are equal. Today I will share with you the method of creating arrays in batches Based on js. Let's take a look.
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
This method consumes a lot of time on the map operation. After removing the map operation, it only takes 2 ms.
Use apply
Here we use a pseudo array of {length: 100000}. Both NodeList and arguments are pseudo arrays (array-like object). They are not actually arrays, objects with the "length Attribute" and "index attribute" at the same time cannot directly use the array methods, but apply and call can accept this pseudo array. The Array. prototype. slice (arguments) we usually use is based on this principle.
Here, the pseudo Array with a length of 100000 is passed to the Array function, and an Array with a length of 100000 is constructed, and then assigned a value using map. Some may ask why the Array (100000) is not directly generated by Array (100000). This is because each value of the Array generated by Array () is undefined and cannot be traversed by map.
Use Array. from ()
This is a new method of ES6, which can directly convert a pseudo array to an array.
If you replace a pseudo array with an array, the speed will decrease a lot.
Use Array. fill ()
First fill the Array with Array. fill (), and then assign values by map
Use for Loop
I said I was shocked at the time, and I was still checking whether there was less 0. I said I was not satisfied, and I wanted to try it with push.
Push is also fast ......
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 above section details how to create arrays in batches using JavaScript (Figure). For more information, see other related articles in the first PHP community!