在数组的前面插入有序值流的有效方法

假设我有一个包含n个有序元素的数组。

假设我有一个相同顺序的传入元素流,所有这些元素都在当前数组中的元素之前。

例如,数组包含降序排列的n条消息,并且流包含新消息。

天真的解决方案是使用array.unshift(x)

但是在我的情况下,n很大,许多元素将通过流进入。

所以我想避免对每个传入元素执行array.unshift,因为我猜想这将需要调整数组的大小和一个副本,因此需要O(n)。

So I'm thinking of statically initializing the array to, say, 2n like this new Array(2n), loading the n elements in the back half of the array and inserting each incoming element at the position just prior to position occupied by the first element in ordered set current in the array.

所以我将O(1)插入物换成O(n)插入物。

当然,我将不得不在某个时候调整数组的大小,但是在所有插入的数组插入中分摊了费用。

I'm asking here because the internet does not look kindly on new Array and also I'm looking for the right way to do this in javascript.