you can create an array of 10, 000 items, such as const newArry = new Array (10000). Fill ('test')
and then a for
loop pushes the array's test
into your items
array each time, and then another loop
if the previous loop uses unshift
, use the concat
loop. Code >
before the start of the loop new Date ()
save a time after the end of the cycle new Date ()
get a new time with the new time minus the old time, you can get the running time. You can see which is faster and which is slower
in terms of the same operation. The performance of concat is much better than that of unshift ide-an-array" rel=" nofollow noreferrer "> performance test
can be flipped through reserve
, then push
, and then reserve
flip, which should be the best
.
I think it seems understandable. I don't know if this is the right way to understand.
Array (item). Concat (list)
is equivalent to merging this array with the newly generated array, so that the array is generated twice:
- list is converted to Array, resulting in an array with only one element of item. Because there is little content, the generation speed is relatively fast
- generated
[item,...]
, which is a time-consuming process
reverse (list); list.push (item); reverse (list);
is equivalent to generating an array twice and then push:
- reverse generates an array of
[- 1.. 0]
, which is time-consuming
- push adds a new element to the array
[- 1... 0, item]
this process is very fast
- again reverse generates an array of
[item, 0,...-1]
, which is time-consuming .
list.unshift (item)
it traverses the array only once:
- first add
list.length
to 1
- iterates through the array once and moves the element back, which is a very time-consuming process
-
list [0] = item
this saves the most time
so when there are more and more elements, unshift is slow in nature, but compared with the first two, it only traverses elements once and does not generate new elements, so when there are more than 100000 elements, unshift is the fastest single-minded method.