Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Array.prototype.push faster than variable declaration

I ran the following jsperf https://jsperf.com/push-vs-define-anaoum

and found out that push in this case :

var a = [];
a.push([1,2,3]);
a.push(["a","b","c"]);

is faster than just declaring a with the subarrays inside :

var a = [
  [1,2,3],
  ["a","b","c"]
];

Could anyone please tell me why or point me in the right documentation?

Thanks enter image description here

like image 819
Anthony N. Avatar asked Jul 04 '26 23:07

Anthony N.


1 Answers

For me declaring a with sub arrays went faster than push :)

I think it depends on computer's/device's components

My example

like image 123
Mr Phoenix Avatar answered Jul 07 '26 12:07

Mr Phoenix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!