最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 5 - Does JavaScript (ECMAScript5) Strict Mode offer significant performance advantages to merit widespread use? - Sta

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jan 25, 2011 at 21:48 Phil.WheelerPhil.Wheeler 16.9k10 gold badges102 silver badges157 bronze badges 1
  • For the sake of pleteness, see Annex C of the ECMAScript 5 specification. – Marcel Korpel Commented Jan 25, 2011 at 22:20
Add a ment  | 

3 Answers 3

Reset to default 6

Well, strict mode code can certainly perform better because it removes issues that made optimization harder, for example, from the top of my head:

  • The with statement was removed (Really difficult -if not impossible- to optimize).
  • No more undeclared assignments, and other prohibitions, e.g. (delete varName;)
  • eval does not introduce variable/function declarations into the local scope.
  • arguments.callee was removed, (difficult to optimize (e.g. function inlining))
  • The arguments object index named properties are not anymore dynamically mapped to the named formal parameters.

I think the reasons to use it were spelled out well by John Resig, http://ejohn/blog/ecmascript-5-strict-mode-json-and-more/, and it appears Firefox will be supporting it, http://whereswalden./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 mon 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 pile 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 piler.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论