Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 2 down vote accepted

push is way faster, almost 300% faster.

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

share|improve this answer
2  
then try this. jsperf.com/index-vs-push/2 :) –  naveen Jul 21 '11 at 7:50
2  
i ran the test twice (firefox) and in both case push is slower (about 6%). However in Chrome is different (push is 50% fast). Also Firefox is fast than Chrome (for this test) using the Latest firefox and chrome. –  magallanes Aug 30 '11 at 18:49
    
Odd. Seems like for most modern browsers (Chrome >= 19), length is way faster. See the browserscope box on the jsperf page. –  Timothy Gu Nov 11 '14 at 4:40
    
@magallanes Chrome 19 seems to boost the performance of the length method a lot, so much it is now over 1.5 times faster than push() on small arrays. –  Timothy Gu Nov 11 '14 at 4:44

Since arrays in JavaScript do not have holes the functionality of those two methods is equal. And yes, using .push() is much cleaner (and shorter).

share|improve this answer
1  
Theoretically, yes. But looking at the jsperf made by @AlienWebguy and @naveen .push() is much slower. But in any case this kind of microoptimization probably doesn't worth much in real life. –  Timothy Gu Nov 11 '14 at 4:42

I've generally thought length assignment was faster. Just found Index vs. push performance which backs that up; for my Chrome 14 browser anyway, over a single test run. However there is not much in it in Chrome.

share|improve this answer
    
omg.. push is slower.. both in firefox 5.0 and chrome –  naveen Jul 21 '11 at 7:48
    
What version of Chrome? Are you saying that push (the 2nd test) is slower than the 1st or 3rd test? –  kzh Sep 12 '11 at 14:43

There seems to be discrepancy on which test is faster among the varying JavaScript engines. The differences in speed may be negligible (unless an unholy amount of pushes are needed). In that case, the prudent developer should always err on the side of readability. In this case, in my opinion and the opinion of @TheifMaster is that [].push() is cleaner and it is easier to read. Maintenance of code is the most expensive part of coding.

share|improve this answer
2  
+1 for mentioning the importance of this: "Maintenance of code is the most expensive part of coding" –  ajax333221 Feb 16 '12 at 4:50

As I tested, the first way is faster, I'm not sure why, keep researching. Also the ECMA doesn't mentioned which one is better, I think it is depending on how the browser vendor implements this.

var b = new Array();
var bd1 = new Date().getTime();
for(var i =0;i<1000000; i++){
    b[b.length] = i;
};

alert(new Date().getTime()- bd1);

var a = new Array();
var ad1 = new Date().getTime();
for(var i =0;i<1000000; i++){
    a.push(i);
};

alert(new Date().getTime()- ad1);
share|improve this answer
    
Are you saying that myArray[myArray.length] = val1; is faster? What browser are you using? –  kzh Sep 12 '11 at 14:42
    
The result that: the first case is much faster in Chrome and Firefox, but litter faster in IE9, I don't have a lower version of IE; so in all, it's faster in these browsers. –  Howard Sep 13 '11 at 0:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.