Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  144] [ 4]  / answers: 1 / hits: 18312  / 13 Years ago, thu, july 21, 2011, 12:00:00

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?


More From » javascript

 Answers
30

push is way faster, almost 300% faster.



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


[#91087] Tuesday, July 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shantelc

Total Points: 737
Total Questions: 120
Total Answers: 104

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;