[英]Is strict mode more performant?
Does executing javascript within a browser in 'strict mode' make it more performant, in general?一般来说,在“严格模式”下在浏览器中执行 javascript 会提高性能吗? 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.但我可以肯定地说,严格模式确实提供了这些机会,浏览器会实现它们——不管提供这些机会是否是 ECMA 委员会的一个有意目标。 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.) (我在 Mozilla 的 JavaScript 引擎上工作并实现了严格模式的各个部分,我们以这种方式实现它作为一般规则——尽管如果我尝试过,我可能会想到一两个例外。)
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.关于性能,我觉得浏览器厂商现在很难实现严格模式,问题是JS引擎大多基于ECMAScript 3,实现严格模式并不容易,因为严格的范围非常灵活,您可以混合使用非严格和严格的代码。
See also:也可以看看:
According to this test "strict mode" can be about 25% faster.根据这个测试,“严格模式”可以快 25% 左右。
<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这可以在这里测试: 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这是 jsPerf 测试: 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:如果您仔细检查 ECMAScript 5 标准文档,您会注意到伪代码算法中几乎所有出现的严格模式都相当于:
if (isStrictMode) {
//throw an (early) SyntaxError or TypeError
}
else {
//return
}
There's two things to note about this:对此有两点需要注意:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.