评论会影响性能吗

评论会影响性能吗

本文介绍了评论会影响性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否说JavaScript代码没有编译,不是JIT?如果是,这是否意味着评论会影响性能,我应该非常小心,我把我的评论?例如,如果可能的话,在函数定义之上和之外放置函数注释,并且绝对避免在循环中放置注释,如果我想最大化性能?我知道在大多数情况下(至少在非循环的情况下),性能的变化可以忽略不计,但我认为这是一个很好的知道和意识到的东西,特别是用于前端/ js开发人员。此外,我最近对js评估提出了相关问题。

解决方案

否。虽然JavaScript是传统的插入语言(虽然不一定是),一些引擎,甚至可能是大多数,在一定程度上在运行。 V8(Chrome和NodeJS中的引擎)在两个步骤的过程中做得非常彻底(第一步应用标准优化;如果它在代码中看到热点,它会用积极的优化重新编译该部分)。即使是不太积极的引擎,几乎肯定至少会将文本解析成某种形式的字节码。



请记住,解释和编译通常更多的是环境事物比语言事物;有C解释器,还有JavaScript编译器。语言往往与环境紧密相关(例如,JavaScript趋向于与Web浏览器环境相关联,即使它的使用总是比这更广泛,甚至在1995年),但即使如此(如我们所见)

在初始解析阶段,非常,非常,非常最小的一个。



如果你真的担心它,你可以使用工具缩小你的脚本,例如或(即使只是简单的优化)。前者只会删除评论和不必要的空白,这样的东西(仍然相当有效);后者确实实际上理解代码并做一些内联等。所以你可以自由地注释,然后使用这些工具,以确保当脚本第一次加载时,这些注释可能有微小的影响通过使用缩小工具绕过。



当然,关于JavaScript性能的事情是,很难预测可靠的交叉引擎,因为引擎变化这么多。所以实验很有趣:





p>结果?我的看法是,在测试的测量误差范围内没有可辨别的差异。


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 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. Also, a relevant question was asked on a js assessment I recently took.

解决方案

No. Although JavaScript is traditionally an "interprted" language (although it needn't necessarily be), some engines, probably even most, compile it on-the-fly to some degree. V8 (the engine in Chrome and NodeJS) does so really quite thoroughly, in a two-step process (the first step applies standard optimizations; if it sees "hot spots" in the code, it recompiles that portion with aggressive optimizations). Even engines that are less aggressive almost certainly at least parse the text into some form of bytecode.

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.

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.

这篇关于评论会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:38