How can I add new array elements at the beginning of an array in Javascript?

前端 未结 13 772
一生所求
一生所求 2020-11-22 13:31

I have a need to add or prepend elements at the beginning of an array.

For example, if my array looks like below:

[23, 45, 12, 67]

13条回答
  •  我在风中等你
    2020-11-22 14:06

    If you need to continuously insert an element at the beginning of an array, it is faster to use push statements followed by a call to reverse, instead of calling unshift all the time.

    Benchmark test: http://jsben.ch/kLIYf

提交回复
热议问题