Giter Site home page Giter Site logo

Comments (3)

yeahsgz avatar yeahsgz commented on July 29, 2024

知道数组的长度,用下标赋值比push要快;但是数组的长度更改耗时比较大。在chrome上小测了一下是这样子

from underscore-analysis.

Arvinzhu avatar Arvinzhu commented on July 29, 2024
  var flatten = function(input, shallow, strict, output) {
    output = output || [];
    var idx = output.length;
    for (var i = 0, length = getLength(input); i < length; i++) {
      var value = input[i];
      if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
        // Flatten current level of array or arguments object.
        if (shallow) {
          var j = 0, len = value.length;
          while (j < len) output[idx++] = value[j++];
        } else {
          flatten(value, shallow, strict, output);
          idx = output.length;
        }
      } else if (!strict) {
        output[idx++] = value;
      }
    }
    return output;
  };

github上面最新的代码发生了一些变化,第四个参数不是startIndex,而是输出数组output。循环初始值i直接就是0。那么这个传入的output,好像没什么意义,传入一个非空数组也并不会处理,不懂这样改有什么意义

from underscore-analysis.

L-Chris avatar L-Chris commented on July 29, 2024

@Arvinzhu 我的想法是
1.首先,去除startIndex,实际应用没有这样的需求
2.output改为参数传入,修改output时就不用寻址到顶部的output,还去除了value重新赋值,效率更高

from underscore-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.