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
There is special method:
a.unshift(value);
But if you want to prepend several elements to array it would be faster to use such a method:
var a = [1, 2, 3],
b = [4, 5];
function prependArray(a, b) {
var args = b;
args.unshift(0);
args.unshift(0);
Array.prototype.splice.apply(a, args);
}
prependArray(a, b);
console.log(a); // -> [4, 5, 1, 2, 3]