简体   繁体   English

评论会影响绩效吗?

[英]Do comments affect performance?

Am I correct to say that JavaScript code isn't compiled, not even JIT? 我是否正确地说JavaScript代码没有编译,甚至不是JIT? If so, does that mean that comments have an affect on performance, and I should be very careful where I put place my comments? 如果是这样,这是否意味着评论会对绩效产生影响,我应该非常小心地将评论放在哪里? Such as placing function comments above and outside the function definition when possible, and definitely avoid placing comments inside loops, if I wanted to maximize performance? 比如在可能的情况下在函数定义的上方和外部放置函数注释,并且如果我想最大化性能,肯定避免在循环中放置注释? I know that in most cases (at least in non-loop cases), the change in performance would be negligible, but I think that this would be something that is good to know and be aware of, especially for front-end/js developers. 我知道在大多数情况下(至少在非循环的情况下),性能的变化可以忽略不计,但我认为这将是一个很好的知识和意识,特别是对于前端/ js开发人员。 Also, a relevant question was asked on a js assessment I recently took. 另外,我最近对js评估提出了一个相关问题。

Am I correct to say that JavaScript code isn't compiled, not even JIT? 我是否正确地说JavaScript代码没有编译,甚至不是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. 虽然JavaScript传统上是一种“解释”语言(虽然它不一定必须如此),但大多数JavaScript引擎在必要时即时编译它。 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); V8(Chrome和NodeJS中的引擎)用于立即快速编译,然后返回并积极优化任何使用过的代码(旧的FullCodegen + TurboFan堆栈); 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. 一段时间后, 他们做了大量的实际测量, 他们切换到最初解析为byteocde和解释,然后编译代码是否被重用(新的Ignition + TurboFan堆栈),通过不编译运行获得显着的内存节省-once设置代码。 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. 有C语言解释器,还有JavaScript编译器。 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. 语言往往与环境紧密相关(就像JavaScript与Web浏览器环境的关系一样,即使它总是被广泛使用,甚至早在1995年),但即便如此(如我们所见),可能有变化。

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). 但是,如果您真的担心它,可以使用jsminClosure Compiler等工具缩小脚本(即使只进行简单的优化)。 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. 当然,关于JavaScript性能的事情是,很难可靠地预测跨引擎,因为引擎变化很大。 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. 我的看法是测试的测量误差中没有明显的差异。

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. 因此,为什么所有专业网站都使用最小化器来生产高效版本,将js缩小到最小。

It may have some effect. 它可能会有一些影响。 Very minimalistic effect, though (even IE6 handles comments correctly ! to be confirmed... ). 虽然非常简约的效果(甚至IE6正确处理评论! 待确认...... )。

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. V8通过在执行之前将JavaScript编译为本机机器代码来提高性能。

Source 资源

可以防止内联函数 ,这会影响性能,但这不应该发生。

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. 我正在编写一个冗长的用户脚本,使用TamperMonkey在Mac上的最新Firefox中使用,并且当我从脚本中删除了冗长的评论并且突然脚本执行完全停止在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. 在新用户帐户上运行相同的完整脚本的多个来回比较,唯一的区别是评论,证明了这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM