javascript var statement and performance

前端 未结 7 1515
后悔当初
后悔当初 2021-01-18 10:29

Option1 : multiple var without assignment

function MyFunction() {

  var a = null;
  var b = null;
  ....
  var z = null;

  a = SomeValue;
         


        
相关标签:
7条回答
  • 2021-01-18 11:08

    In Chrome, execution times are identical.

    The only real consideration here is optimizing network transfer.

    Consider var a=1;var b=2;...;var z=9; versus var a=1,b=2,...,z=9;

    If you put a ;var  in front of each identifier, that's 5 bytes (assuming a single-byte character encoding), versus 1 byte for a ,. Thus, declaring 26 variables, you can save 100 bytes by writing var  once and listing identifiers with commas.

    Granted it's not a huge savings, but every byte helps when it comes to pushing bits over the network. It's not something to worry a great deal about, but if you find yourself declaring several variables in the same area, using the variable declaration list is an easy way to shave a few bytes off your JS file.

    0 讨论(0)
提交回复
热议问题