省略分号会影响JavaScript的性能吗? [英] Does omitting semicolons affect performance in JavaScript?

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

问题描述

我正在与一位同事讨论JavaScript,同时查看了一些代码片段.我们注意到这些代码片段在语句末尾缺少; .我们都知道,即​​使在行尾没有显示分号,JS也可以正确解释,但是我想知道这是否会影响评估的性能,因为它是一种解释语言.

I was discussing with a colleague about JavaScript while looking at some snippets. 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.

推荐答案

包含空格,分号和注释的javascript文件比较重.那是主要的影响.

A javascript file with spaces, semi-colons and comments is heavier. 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.

如果您想拥有尽可能轻的代码,请使用 minifier 为浏览器构建更简洁的代码.这是它的职责,而不是你的职责.

And use a minifier to build a more concise code for the browser if you want to have the lightest possible code. It's its duty, not yours.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆