Do comments affect performance?

后端 未结 5 578
囚心锁ツ
囚心锁ツ 2021-02-05 11:05

Am I correct to say that JavaScript code isn\'t compiled, not even JIT? If so, does that mean that comments have an affect on performance, and I should be very careful where I p

相关标签:
5条回答
  • 2021-02-05 11:24

    The biggest effect that comments have is to bloat the file size and thereby slow down the download of the script. Hence why all professional sites use a minimizer for a productive version to cut the js down to as small as it gets.

    0 讨论(0)
  • 2021-02-05 11:29

    In some perhaps isolated circumstances, comments definitely somehow bog down the code execution. I am writing a lengthy userscript, using in the latest Firefox on Mac using TamperMonkey, and several days' increasingly frustrated troubleshooting came to an end when I stripped the lengthy comments from the script and suddenly the script execution stopped hanging completely on Facebook. Multiple back-and-forth comparisons running the same exact script on a fresh user account, the only difference being the comments, proved this to be the case.

    0 讨论(0)
  • 2021-02-05 11:34

    It may have some effect. Very minimalistic effect, though (even IE6 handles comments correctly ! to be confirmed...).

    However, most people use a minifier that strips off comments. So it's okay.

    Also:

    V8 increases performance by compiling JavaScript to native machine code before executing it.

    Source

    0 讨论(0)
  • 2021-02-05 11:37

    It can prevent functions from being inlined, which affects performance, though this shouldn't really happen.

    0 讨论(0)
  • 2021-02-05 11:47

    Am I correct to say that JavaScript code isn't compiled, not even JIT?

    No. Although JavaScript is traditionally an "interpreted" language (although it needn't necessarily be), most JavaScript engines compile it on-the-fly whenever necessary. V8 (the engine in Chrome and NodeJS) used to compile immediately and quickly, then go back and aggressively optimize any code that was used a lot (the old FullCodegen+TurboFan stack); a while back having done lots of real-world measurement, they switched to initially parsing to byteocde and interpreting, and then compiling if code is reused much at all (the new Ignition+TurboFan stack), gaining a significant memory savings by not compiling run-once setup code. Even engines that are less aggressive almost certainly at least parse the text into some form of bytecode, discarding comments early.

    Remember that "interpreted" vs. "compiled" is usually more of an environmental thing than a language thing; there are C interpreters, and there are JavaScript compilers. Languages tend to be closely associated with environments (like how JavaScript tends to be associated with the web browser environment, even though it's always been used more widely than that, even back in 1995), but even then (as we've seen), there can be variation.

    If so, does that mean that comments have an affect on performance...

    A very, very, very minimal one, on the initial parsing stage. But comments are very easy to scan past, nothing to worry about.

    If you're really worried about it, though, you can minify your script with tools like jsmin or the Closure Compiler (even with just simple optimizations). The former will just strip comments and unnecessary whitespace, stuff like that (still pretty effective); the latter does that and actually understands the code and does some inlining and such. So you can comment freely, and then use those tools to ensure that whatever minuscule impact those comments may have when the script is first loaded is bypassed by using minifying tools.

    Of course, the thing about JavaScript performance is that it's hard to predict reliably cross-engine, because the engines vary so much. So experiments can be fun:

    • Here's an experiment which (in theory) reparses/recreates the function every time
    • Here's one that just parses/creates the function once and reuses it

    Result? My take is that there's no discernable difference within the measurement error of the test.

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