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 + 1) steps?
In code, essentially, what I currently am doing is
function prependArray(value, oldArray) {
var newArray = new Array(value);
for(var i = 0; i < oldArray.length; ++i) {
newArray.push(oldArray[i]);
}
return newArray;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…