简体   繁体   English

省略分号会影响JavaScript的性能吗?

[英]Does omitting semicolons affect performance in JavaScript?

I was discussing with a colleague about JavaScript while looking at some snippets. 我正在与一位同事讨论JavaScript,同时查看一些代码片段。 We noticed that these snippets were missing the ; 我们注意到这些片段错过了; at the end of the statements. 在声明的最后。 We all know that JS is interpreted correctly even if no semicolon is shown at the end of a line, but I was wondering if this affects somehow the performance of evaluation, since it is an interpreted language. 我们都知道即使在行的末尾没有显示分号也能正确解释JS,但我想知道这是否会影响评估的性能,因为它是一种解释型语言。

A javascript file with spaces, semi-colons and comments is heavier. 带有空格,分号和注释的javascript文件比较重。 That's the main impact. 这是主要影响。

But you're a coder and you have to maintain the code, so this very slight impact is much less important than the adverse one on readability. 但是你是一个编码器,你必须维护代码,所以这个非常小的影响远不如可读性上的不利影响那么重要。 And omitting the semicolons means you know when you can omit them. 省略分号意味着你知道什么时候可以省略它们。 But the rules aren't so simple and learning them isn't worth your time. 但规则并不那么简单,学习它们不值得你花时间。

Leave the semi-colons where they are, you'll avoid bugs. 将分号留在原地,你就可以避免虫子。

And use a minifier to build a more concise code for the browser if you want to have the lightest possible code. 如果您想拥有最轻的代码,请使用minifier为浏览器构建更简洁的代码。 It's its duty, not yours. 这是它的职责,而不是你的责任。

Omitting semicolons in JS is big debate, but we should always keep semicolon. 在JS中省略分号是一个很大的争论,但我们应该始终保持分号。 If you talk about performance, then there will be very little benefit while keeping semicolon. 如果你谈论性能,那么在保持分号的同时几乎没有什么好处。

But the things not end up here only. 但事情并非仅限于此。 Other than performance there is one big thing which needs to be take care. 除了表演之外,有一件大事需要注意。

Doug Crockford explains the need of semicolons very well in this presentation : Doug Crockford在本演示文稿中非常清楚地解释了分号的需要:

The JS Interpreter finds an error, adds a semicolon, and runs the whole thing again. JS解释器发现错误,添加分号,然后再次运行整个事件。 But not every time he puts the semicolon on the right place and hilarious bugs are the consequence. 但并不是每次他都把分号放在正确的地方,结果就是搞笑的错误。 You should always make semicolons and run your js through testing tools like JSLint. 你应该总是使用分号并通过JSLint之类的测试工具来运行你的js。

Other than this semicolons give the code more structure and make it cleaner - additionally, their presence allows some developers to obfuscate their code. 除了这个分号之外,还为代码提供了更多结构并使其更清晰 - 此外,它们的存在允许一些开发人员对其代码进行模糊处理。

Hope it will help you. 希望它会对你有所帮助。

Referring to the test provided: That really doesn't illustrate the performance difference of ASI vs non ASI. 参考提供的测试:这实际上没有说明ASI与非ASI的性能差异。 The performance that will be impacted is likely in tokenization. 将受到影响的性能可能是标记化。 The interpreter or JIT won't be doing this for each iteration, but only once. 解释器或JIT不会为每次迭代执行此操作,而只会执行一次。 The effect would likely only be noticed when loading a large code base. 只有在加载大型代码库时才会注意到这种效果。 The semicolon allows the JIT or interpreter to more quickly recognize an end-of-statement than inferring it from the next tokens. 分号允许JIT或解释器更快地识别语句结束而不是从下一个标记推断它。

Tiny differences in code syntax usually have very little effect on the performance of the code. 代码语法的微小差异通常对代码的性能影响很小。 the process of interpreting a string of code is very efficient and takes a tiny fraction of the time taken by actually running the code. 解释一串代码的过程非常有效,并且只花费了实际运行代码所花费的时间的一小部分。

An inefficient algorithm or an extra network call, such as pulling in multiple .js files instead of a single one have far greater impact. 低效算法或额外的网络调用(例如,拉入多个.js文件而不是单个文件)会产生更大的影响。

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

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