Most efficient way to prepend an array in Javascript?

I'm not sure about more efficient in terms of big-O but certainly using the unshift method is more concise (and therefore probably easier to understand): var a = 1, 2, 3, 4; a. Unshift(0); a; // => 0, 1, 2, 3, 4 Edit This jsPerf benchmark shows that unshift is decently faster in at least a couple of browsers, regardless of possibly different big-O performance iff you are ok with modifying the array in-place. If you really can't mutate the original array then you would do something like the below snippet, which doesn't seem to be appreciably faster than your solution: a.

Slice(0). Unshift(0); // Use "slice" to avoid mutating "a.

I'm not sure about more efficient in terms of big-O but certainly using the unshift method is more concise (and therefore probably easier to understand): var a = 1, 2, 3, 4; a. Unshift(0); a; // => 0, 1, 2, 3, 4 Edit This jsPerf benchmark shows that unshift is decently faster in at least a couple of browsers, regardless of possibly different big-O performance iff you are ok with modifying the array in-place. If you really can't mutate the original array then you would do something like the below snippet, which doesn't seem to be appreciably faster than your solution: a.

Slice(0). Unshift(0); // Use "slice" to avoid mutating "a".

1, deleted my post in favour of this. It's certainly more efficient than what the op has, built-in functions always are. – Andy E Jun 1 at 2:53 ah bingo, must have forgotten about this one, I wonder how it stacks up against efficiency vs what I was doing – samccone Jun 1 at 2:54 I'm sure unshift is still O(N), due to the storage of arrays.

But yes, it would be much more efficient than the loop. – mgiuca Jun 1 at 2:56.

If you are prepending an array to the front of another array, it is more efficient to just use concat. So: var newArray = values. Concat(oldArray); But this will still be O(N) in the size of oldArray.

Still, it is more efficient than manually iterating over oldArray. Also, depending on the details, it may help you, because if you are going to prepend many values, it's better to put them into an array first and then concat oldArray on the end, rather than prepending each one individually. There's no way to do better than O(N) in the size of oldArray, because arrays are stored in contiguous memory with the first element in a fixed position.

If you want to insert before the first element, you need to move all the other elements. If you need a way around this, do what @GWW said and use a linked list, or a different data structure.

Oh yes, I forgot about unshift. But note that a) that mutates oldArray whereas concat doesn't (so which one is better for you depends on the situation), and b) it only inserts one element. – mgiuca Jun 1 at 2:56 2 This is sooo much slower than unshift: jsperf.Com/prepend12345 – david Jun 1 at 3:01 Wow, that is much slower.

Well, as I said, it is making a copy of the array (and also creating a new array 0), whereas unshift is mutating it inplace. But both should be O(N). Also cheers for the link to that site -- looks very handy.

– mgiuca Jun 1 at 3:03.

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, be = 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.

1 No... unshift will add array as the first argument: var a = 4, 5; a. Unshift(1,2,3); console. Log(a); // -> 4, 5, 1, 2, 3 – bjornd Jun 1 at 3:16 1 @david: not quite, unshifting an array would result in 4, 5, 1, 2, 3... – maerics Jun 1 at 3:16 You are correct!

You would need to use Array.prototype.unshift. Apply(a,b); – david Jun 1 at 3:28.

F you need to preserve the old array, slice the old one and unshift the new value(s) to the beginning of the slice. Var oldA=4,5,6; newA=oldA. Slice(0); newA.

Unshift(1,2,3) oldA+'\n'+newA /* returned value: 4,5,6 1,2,3,4,5,6.

If you are prepending an array to the front of another array, it is more efficient to just use concat. But this will still be O(N) in the size of oldArray. Still, it is more efficient than manually iterating over oldArray.

Also, depending on the details, it may help you, because if you are going to prepend many values, it's better to put them into an array first and then concat oldArray on the end, rather than prepending each one individually. There's no way to do better than O(N) in the size of oldArray, because arrays are stored in contiguous memory with the first element in a fixed position. If you want to insert before the first element, you need to move all the other elements.

If you need a way around this, do what @GWW said and use a linked list, or a different data structure.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions