Most efficient way to prepend a value to an array

后端 未结 9 1094
予麋鹿
予麋鹿 2020-12-02 04:53

Assuming I have an array that has a size of N (where N > 0), is there a more efficient way of prepending to the array that would not require O(N

9条回答
  •  不思量自难忘°
    2020-12-02 05:19

    Example of prepending in-place:

    var A = [7,8,9]
    var B = [1,2,3]
    
    A.unshift(...B)
    
    console.log(A) // [1,2,3,7,8,9]

提交回复
热议问题