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

Viewed 17347

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?

7 Answers

I have an updated benchmark here: jsbench.me Feel free to check which is faster for your current engine. arr[arr.length] was about 40% faster than arr.push() on Chromium 86.

Related