12

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?

Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • For the sake of completeness, see [Annex C](http://www.ecma262-5.com/ELS5_Annex_C.htm) of the ECMAScript 5 specification. – Marcel Korpel Jan 25 '11 at 22:20

3 Answers3

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.
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

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.

James Black
  • 41,583
  • 10
  • 86
  • 166
  • I understand Chrome / Webkit is supporting it fully in a future release soon. – Phil.Wheeler Jan 25 '11 at 21:59
  • For me it is simple, if John Resig likes it, it must be a good idea. :) – James Black Jan 25 '11 at 22:05
  • 2
    Another link to that list http://hacks.mozilla.org/2011/01/ecmascript-5-strict-mode-in-firefox-4/ – mhitza Jan 25 '11 at 22:24
  • 5
    Note that you don't lose `eval` under strict mode code, the only thing that changes is that a *direct* `eval` call on strict mode code builds a new *variable environment*, meaning that all variable and function declarations do not affect the enclosing scope, they are made in this new environment (thing that obviously helps on code optimization made by engines). – Christian C. Salvadó Jan 25 '11 at 23:05
  • @CMS - Thank you for the clarification. I was thinking, as I was learning about eval, that the idea of just downloading javascript to get new functionality for a portal won't work now, so the main use I had for eval is gone. – James Black Jan 26 '11 at 00:44
0

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.

luiscolorado
  • 1,525
  • 2
  • 16
  • 23