Mobile app version of vmapp.org
Login or Join
Marchetta884

: Declaring variables using single or multiple var statements. Performance wise Someone once told me that it's better for Javascript performance to use one var statement rather then multiple. I.e.

@Marchetta884

Posted in: #Javascript #Performance

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

I.e.

// 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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Marchetta884

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme