Skip to content
Advertisement

Using the push method or .length when adding to array?

What are the downsides to doing:

var myArray = [];
myArray[myArray.length] = val1;
myArray[myArray.length] = val2;

instead of:

var myArray = [];
myArray.push(val1);
myArray.push(val2);

I’m sure the push method is much more “acceptable”, but are there any differences in functionality?

Advertisement

Answer

push is way faster, almost 300% faster.

Proof: http://jsperf.com/push-vs-length-test

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement