3

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

5
  • Which browser did you use? I just tried in Edge and the push came out 20% slower. Commented Aug 11, 2017 at 18:22
  • @KirkLarkin Chrome 60.0.3112 / Windows 7 0.0.0 Commented Aug 11, 2017 at 18:36
  • .push() Firefox 55 on Windows 10 came out 23% slower but Chrome 61 came out 75% faster (similar results). Commented Aug 11, 2017 at 18:42
  • @Pluto yeah, I wouldn't say I'm surprised but I would still love to read documentation or articles on the subject. Optimization is very important to me Commented Aug 11, 2017 at 20:20
  • 1
    for me on Chrome 61 push is about 6.5 times faster too on jsperf.com, but slower on jsben.ch, so depends on how it's measured too Commented Oct 12, 2017 at 4:11

1 Answer 1

1

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

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

My example

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using Chrome 60.0.3112 / Windows 7 0.0.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.