Does code removal efficiency increase? Javascript

Does removing comments from JavaScript code improve performance?

I understand that this is not a good programming practice, since comments are an integral part of development. I'm just curious to know if they really do some overhead at compile time.

+5
source share
7 answers

If you compile or interpret your JavaScript, the compiler / interpreter should look at the line, solve its comment and move on (or look at the line area). For web applications, you also need to load comment lines.

So there is some overhead.

, , , .

, , .

+11

Javascript .

, .

, JSMin , -. ( SURE, , ).

+7

? . bytesize, .

. . javascript.

, javascript , HTTP-. HTTP- .

+3

Update

24 2016 .

: https://github.com/v8/v8/commit/0702ea3000df8235c8bfcf1e99a948ba38964ee3#diff-64e6fce9a2a9948942eb00c7c1cb75f2


!

, , , , , .

, JavaScript - , , .

( 2016 ) JavaScript-, V8, , . , , JIT - Just In Time. JIT , .

, :

function doIt(a, b) {
  return (a + b) * 2;
}

function loop() {
  var x = 1, y = 1;
  var i;
  for(i = 0; i < 100; ++i) {
    x = doIt(x, y);
  }
}

:

function loop() {
  var x = 1, y = 1;
  var i;
  for(i = 0; i < 100; ++i) {
    // the doIt call is now gone, replaced with inlined code
    x = (x + y) * 2;
  }
}

JIT , , doIt . , .

, JavaScript , ? , - . , V8 , , .

, , V8, .

, :

https://top.fse.guru/nodejs-a-quick-optimization-advice-7353b820c92e#.uoply32op

, ; V8 TurboFan, .

( ) TurboFan, :

https://github.com/v8/v8/blob/5ff7901e24c2c6029114567de5a08ed0f1494c81/src/compiler/js-inlining-heuristic.cc#L55

+3

JavaScript.

: JavaScript, . , JavaScript. , .

, "".js , . Minifiers , . JavaScript , , , .

+2

, " - , ", , .

+1

, , .

You should always have comments in the code you are running in, and use the minifier to split them into deployments - the YUI Compressor is good.

+1
source

All Articles