Kinnard Hockenhull
Kinnard Hockenhull

Reputation: 3020

Difference between declaring an array with elements and iterating elements into a new array?

What is the Difference between declaring an array with elements and declaring it and iterating the elements into it in Javascript?

r = [req.body.email,
     req.body.password];

vs

r=[];
r.push(req.body.email, req.body.password);

Upvotes: 0

Views: 83

Answers (1)

Billy Moon
Billy Moon

Reputation: 58601

Biggest difference is code readability. They are functionally equivalent.

Depending on the javascript engine, one might perform faster than the other, but the difference is negligible.

You can measure the exact performance on jsperf, something like this: http://jsperf.com/assignment-vs-push/7

Run the test in different browsers to see different results, but looks to me like the direct assignment is faster.

Upvotes: 4

Related Questions