Is there any performance difference between concat and unshift of arrays?

for example, suppose there is such a structure

******************110******************
unshift
(): 0.384ms
concat
(): 0.125ms
reverse->push->reverse
(): 0.170ms
******************0******************


******************2100******************
unshift
(): 0.072ms
concat
(): 0.070ms
reverse->push->reverse
(): 0.065ms
******************1******************


******************31000******************
unshift
(): 0.063ms
concat
(): 0.072ms
reverse->push->reverse
(): 0.063ms
******************2******************


******************410000******************
unshift
(): 0.157ms
concat
(): 0.164ms
reverse->push->reverse
(): 0.123ms
******************3******************


******************5100000******************
unshift
(): 1.317ms
concat
(): 2.065ms
reverse->push->reverse
(): 3.261ms
******************4******************


******************61000000******************
unshift
(): 5.075ms
concat
(): 10.130ms
reverse->push->reverse
(): 12.165ms
******************5******************
Jun.05,2022

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:

  1. 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
  2. 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:

  1. reverse generates an array of [- 1.. 0] , which is time-consuming
  2. push adds a new element to the array [- 1... 0, item] this process is very fast
  3. again reverse generates an array of [item, 0,...-1] , which is time-consuming
  4. .

list.unshift (item) it traverses the array only once:

  1. first add list.length to 1
  2. iterates through the array once and moves the element back, which is a very time-consuming process
  3. 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.

Menu