Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Someone once told me that it's better for Javascript performance to use one var statement rather then multiple.


// Version A. Allegedly Faster
var a = 1,
    b = 2,
    c = 3;

// Version B. Allegedly Slower
var a = 1;
var b = 2;
var c = 3;

The reasoning behind this was along the lines of: For every var statement, Javascript will start allocating memory and then stop at the semicolon. Whereas, if you have only one var statement, many JS implementations will optimize it and allocate space for all variables in the same call. Thus making things go faster.

However, when googling to confirm this I only find rants about how some consider the second example to be simpler from a maintenance point of view. And some disagree. This JSPerf test sais there is no difference.

So my question: From a performance perspective, is there any reason version A or B would be better?

share|improve this question

closed as off-topic by w3dk, John Conde Mar 6 '14 at 13:53

  • This question does not appear to be about webmastering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

This question appears to be off-topic because it is about programming which is off-topic at Pro Webmasters. Programming questions may be asked at Stack Overflow but be sure to read their FAQ before posting to ensure your question meets their guidelines. – John Conde Mar 6 '14 at 13:53
This question is regarding client side (browser) programming and web performance. As a bonus, it can be applied to all forms of javascript (i.e. backend). If you don't agree I will move it to stackoverflow. – Skurpi Mar 7 '14 at 12:25
up vote 2 down vote accepted

From a performance perspective, is there any reason version A or B would be better?

You seem to have already answered your question; there is no difference in client-side JavaScript.

Even if there was a difference, it would be a micro-optimisation at best (ie. no relevance, performance-wise, in the real world).

The overriding factor is code readability. If code is easier to read, it is easier to maintain. Use whichever works best for you, for the code you are writing.

share|improve this answer

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