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?
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.)
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:
According to this test "strict mode" can be about 25% faster.
<div id="a">
Q
</div>
<div id="b">
Q
</div>
<script>
Benchmark.prototype.setup = function() {
function d(i) {
var x = '999';
y = eval("y = 8;");
var z = x + y + i;
document.getElementById('a').innerHTML = z;
}
function c(i) {
'use strict'
var x = '999';
var y = eval("y = 8;");
var z = x + y + i;
document.getElementById('b').innerHTML = z;
}
};
</script>
This can be tested here: http://jsperf.com/strict-mode
<script>
Benchmark.prototype.setup = function() {
var nonstrict = (function() {
return function (arg1) {
var index;
for (index = 1; index < arguments.length; ++index) {
arguments[0] += arguments[index];
}
return arguments[0] - arg1;
};
}());
var strict = (function() {
"use strict";
return function (arg1) {
var index;
for (index = 1; index < arguments.length; ++index) {
arguments[0] += arguments[index];
}
return arguments[0] - arg1;
};
}());
var result;
};
</script>
Here's the jsPerf test: http://jsperf.com/strict-mode-arguments
For the most part, no. If you closely examine the ECMAScript 5 standards document, you'll notice that pretty much all occurrences of Strict Mode in the pseudo-code algorithms amount to:
if (isStrictMode) {
//throw an (early) SyntaxError or TypeError
}
else {
//return
}
There's two things to note about this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With