javascript - 泄漏参数
怪我咯
怪我咯 2017-07-05 10:42:40
0
1
815

谁可以帮忙解释一哈 Leaking arguments 的概念

<script>

        Benchmark.prototype.setup = function() {

          
  function otherFunc(a, b) {
    return a + b;
  }
  
  function withArguments(x) {
    var a = arguments;
    return otherFunc.apply(x, Array.prototype.slice.call(a, 1));
  }
  
  function withCopy(x) {
    var a = [];
    var i, len = arguments.length;
    for (i = 1; i < len; i += 1) {
      a[i - 1] = arguments[i];
    }
    return otherFunc.apply(x, a);
  }
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
某草草

传递arguments给任何方法被称为leaking arguments

不幸的是,传递arguments给任何参数,将导致Chrome和Node中使用的V8引擎跳过对其的优化,这也将使性能相当慢。

withArguments 不会被V8优化,withCopy 那种 建议线上环境使用,虽然啰嗦。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!