简体   繁体   English

严格模式性能更好吗?

[英]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


Interestingly, manipulation of the arguments array can be around 6 times faster in "strict mode"! 有趣的是,在“严格模式”下,对参数数组的操作可以快 6 倍左右!

 <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:对此有两点需要注意:

  1. The checks on Strict Mode didn't exist in ECMAScript 3. While it's relatively lightweight, conforming implementations of JavaScript are now running at least one extra conditional check compared to their ECMAScript 3 counterparts. ECMAScript 3 中不存在对严格模式的检查。虽然它相对轻量级,但与 ECMAScript 3 对应物相比,JavaScript 的合规实现现在至少运行了一项额外的条件检查。 Yeah...I know a single check like this burns very few clock cycles, but little things add up是的...我知道像这样的单次检查会消耗很少的时钟周期,但是小事情加起来
  2. Because Strict Mode is primarily a parse time feature of JavaScript, your favorite browser isn't going to show much of a performance decrease when Strict Mode is enabled for some website (eg, SunSpider).因为严格模式主要是 JavaScript 的一个解析时间特性,所以当为某些网站(例如 SunSpider)启用严格模式时,您最喜欢的浏览器不会表现出太大的性能下降。 That is, the performance degrade occurs before code is executed meaning it could be perceptible to end-users but is largely immeasurable using the Date object to measure block execution time也就是说,性能下降发生代码执行之前,这意味着最终用户可能会察觉到它但使用Date对象来测量块执行时间在很大程度上是无法衡量的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM