Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is strict mode more performant?

Tags:

javascript

Does executing javascript within a browser in 'strict mode' make it more performant, in general? Do any of the major browsers do additional optimisation or use any other techniques that will improve performance in strict mode?

To rephrase slightly, is strict mode intended, amongst its other goals, to allow browsers to introduce additional optimisations or other performance enhancements?

like image 736
sje397 Avatar asked Jun 30 '10 01:06

sje397


People also ask

What is the benefit of strict mode?

By changing errors to throw errors, strict mode removes certain JavaScript silent errors. Strict mode repairs mistakes that make it difficult for JavaScript engines to perform optimizations. (It can sometimes execute faster than identical code that's not strict mode.)

Should I enable strict mode?

Strict mode makes several changes to JavaScript semantics. It eliminates silent errors and instead throws them so that the code won't run with errors in the code. It will also point out mistakes that prevent JavaScript engines from doing optimizations.

What is use strict What are the advantages and disadvantages to using it?

what are the advantages and disadvantages to using it? If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.

What happens strict mode?

Strict mode prohibits function statements that are not at the top level of a script or function. In normal mode in browsers, function statements are permitted "everywhere".


2 Answers

Is strict mode intended, amongst its other goals, to allow browsers to introduce additional optimisations or other performance enhancements?

Whether or not it was intended to do this, I'm not sure, although I think the answer is yes.

But I can say with certainty that strict mode does provide these opportunities, and browsers will implement them -- regardless whether providing those opportunities was an intentional goal for the ECMA committee. However, I wouldn't expect all those opportunities to be taken immediately. In many cases the mantra is likely to be correctness first, performance later, because strict mode is not widely used right now. (I work on Mozilla's JavaScript engine and have implemented various parts of strict mode, and we're implementing it this way as a general rule -- although I could probably think of an exception or two if I tried.)

like image 175
Jeff Walden Avatar answered Sep 21 '22 05:09

Jeff Walden


The strict mode is not really about performance, it a strict variant of the language, its main goal is to avoid what are considered to be error-prone features.

Basically its goal is to make the language safer, introducing are a lot of semantical changes, also additional error checking is made, and erros are noisy, in non-strict code things only silently fail.

About performance, I think browser vendors are now having a hard time now implementing strict mode, the problem is that the JS engines are mostly based on ECMAScript 3, and implementing the strict mode is not easy, since the scope of strictness is very flexible, you can mix non-strict and strict code.

See also:

  • ECMAScript 5 Strict Mode, JSON, and More
  • SpiderMonkey strict mode ticket
  • Webkit strict mode ticket
like image 36
Christian C. Salvadó Avatar answered Sep 22 '22 05:09

Christian C. Salvadó