-1

Case 1:

var x = null;
var y = 1;
var z = "hello";

Case 2:

var x = null, y = 1, z = "hello"

Are there any performance difference between the two? Or what's the difference?

AB Udhay
  • 643
  • 4
  • 9
thadeuszlay
  • 2,787
  • 7
  • 32
  • 67
  • 2
    There is no difference. – Mike B Aug 16 '16 at 07:52
  • No performance enhancement, and if it were, it would be extremely minimal. Second case it just quicker and for developers easier to read (in theory). Syntax wise, literally no difference. – Dandy Aug 16 '16 at 07:53
  • Related: [What is the advantage of initializing multiple javascript variables with the same var keyword?](http://stackoverflow.com/q/5455445/218196) – Felix Kling Aug 16 '16 at 07:54
  • 1
    @Dandy - I find the second *harder* to read because it is all on one line. – nnnnnn Aug 16 '16 at 07:54
  • 1
    @Dandy Syntax is *exactly* the difference. Performance and meaning-wise they're identical for all intents and purposes. – deceze Aug 16 '16 at 07:54
  • 1
    Whether the environment processes three variable declarations with one declarator each, or one variable declaration with three declarators doesn't really make a difference. http://www.ecma-international.org/ecma-262/7.0/#sec-functiondeclarationinstantiation – Felix Kling Aug 16 '16 at 08:00
  • @deceze Good point, syntax was the wrong word to use. Thanks for the correction. – Dandy Aug 16 '16 at 08:00
  • I don't understand the downvote for this question.... – thadeuszlay Aug 16 '16 at 08:27

4 Answers4

1

Just writing style. No difference.

At a very fine level, maybe there will be some tokenising difference, but it should not (or rather would not) be a predictable performance variation as it depends on the interpreter/tokeniser implementation or design!!

From my experience, I think you have way more important things than declaration style to worry about for performance tweaking.

Iceman
  • 6,035
  • 2
  • 23
  • 34
0

They are equivalent and there ought to be no difference in performance although that could be down to the Javascript interpreter.

The first way (i.e. three separate statements) is certainly clearer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

None at all it's just a preference; if you are running JsLint it will recommend chaining them together where possible.

http://davidshariff.com/blog/chaining-variable-assignments-in-javascript-words-of-caution/

This is a great blog about variable chaining.

Jonathan Newton
  • 832
  • 6
  • 18
-1

I think there is no real difference between yours two way to do.

If you want a proper code, i think the first is the best, otherwise, the other make you win 2 lines. :)

  • 3
    If there's no real difference then why is the first one "proper code"? – nnnnnn Aug 16 '16 at 07:55
  • Because in some schools or developer team there are rules for coding. That's why if he does not belong to them, it's not important and there is no difference – Tom Lgl-dcl Aug 16 '16 at 08:10