首页 >社区问答列表 >javascript - Leaking arguments

javascript - Leaking arguments

谁可以帮忙解释一哈 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);
  }

  • 某草草
  • 某草草    2017-07-05 10:44:401楼

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

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

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

    +0添加回复

  • 回复