JavaScript (ECMAScript5) 严格模式是否具有显着的性能优势,值得广泛使用?
我正在阅读一些关于使用 JavaScript 严格模式的内容,似乎一般来说,这个想法是向编码器强制执行一组更严格的规则,以确保 JS 引擎可以更好地优化代码。它几乎感觉就像 Visual Basic 中“Option Explicit”的 JavaScript 等价物。
如果这基本上是对我的代码应用严格模式的最终效果,那么性能差异是否值得出于习惯而不是具体情况而应用?除了代码稳定性之外,还有其他值得考虑的优点吗?
我想要对我的脚本应用严格模式的一些关键原因是什么?
I'm reading up a bit on using Strict Mode for JavaScript and it seems that, generally speaking, the idea is to force a more rigid set of rules onto the coder to ensure that the JS engine can optimise the code better. It almost feels like the JavaScript equivalent of "Option Explicit" in Visual Basic.
If this is basically the net effect of applying Strict Mode to my code, would the performance difference be such that it would be worth applying out of habit rather than case-by-case? Are there other advantages besides code stability that might be worth considering?
What are some of the key reasons I would want to apply Strict Mode to my scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,严格模式代码当然可以表现得更好,因为它消除了使优化变得更加困难的问题,例如,从我的脑海中:
with
语句(确实很难 - 如果不是不可能 - 优化)。delete varName;
)eval
不会将变量/函数声明引入本地作用域。arguments.callee
被删除,(难以优化(例如函数内联))arguments
对象索引命名属性不再动态映射到命名形式参数。Well, strict mode code can certainly perform better because it removes issues that made optimization harder, for example, from the top of my head:
with
statement was removed (Really difficult -if not impossible- to optimize).delete varName;
)eval
does not introduce variable/function declarations into the local scope.arguments.callee
was removed, (difficult to optimize (e.g. function inlining))arguments
object index named properties are not anymore dynamically mapped to the named formal parameters.我认为 John Resig 很好地阐述了使用它的原因,http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/,看来 Firefox 将支持它,http://whereswalden.com/2010/09/08/new-es5 -strict-mode-support-now-with-poison-pills/,因此查看可能有用,至少对于库而言。
但是,基本上,它是为了帮助防止一些常见的编程错误,但对于某些人来说,失去 eval 可能是不使用它的理由,对我来说,没有未命名的匿名函数会很困难,但是,任何有助于减少错误的事情都是值得的。
I think the reasons to use it were spelled out well by John Resig, http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/, and it appears Firefox will be supporting it, http://whereswalden.com/2010/09/08/new-es5-strict-mode-support-now-with-poison-pills/, so it may be useful to look at, at least for libraries.
But, basically, it is to help prevent some common programming errors, but for some people losing the
eval
may be reason not to use it, and for me not having unnamed anonymous functions will be difficult, but, anything that can help reduce errors may be worthwhile.我不知道性能是否值得,但我想你的结果可能会有所不同。我想这取决于你的剧本。但这并不意味着成为要点,而是减少维护代码的时间。因此,任何能够节省您维护代码的时间(和金钱)并使其速度更快的东西都是黄金。
我已得到纠正,遗憾的是,它不包括强类型。研究人员花了很多年的时间来强制键入以在编译时检测错误,现在我们必须相信我们的代码是好的,或者通过手工或单元测试来验证它。恕我直言,在单元测试上花费的时间在很多地方通常都很稀缺,并且不应该把它花在编译器可以完成的事情上。
I don't know if the performance would be worthy it, but I guess your results may vary. I suppose it depends on your script. But that doesn't mean to be the main point, but reducing your time in maintaining your code. So anything that makes save you time (and money) maintaining your code, and makes it faster, is golden.
I have been corrected, and, sadly, it doesn't include strong typing. Many years were spent by researchers to enforce typing to detect errors at compile time, and now we have to trust we are code is good, or verify it by hand or unit testing. IMHO, the time spent in unit testing is usually scarce in many places, and it should not be spent on things that could be done by the compiler.