简体   繁体   English

将递归 function 的结果连接到 Javascript 中的数组中的最快方法是什么?

[英]What is the fastest way to concatenate results of a recursive function into an array in Javascript?

As the title says I'm searching for the most efficient way to create a flat array output from a recursive function.正如标题所说,我正在寻找从递归 function 创建平面数组 output 的最有效方法。

The following example produces the correct result but it is kind of slow as it has to create a temporary concatenated array of points for each level of recursion.下面的示例产生了正确的结果,但它有点慢,因为它必须为每个递归级别创建一个临时连接的点数组。

class QuadTree {

  // ...
  // some other methods
  // ...

  get points() {
    if (this.divided) {
      // concatenate and return points of all subtrees
      return this.subtrees[0].points.concat(
        this.subtrees[1].points,
        this.subtrees[2].points,
        this.subtrees[3].points
      );
    }
    // return _points array of this quadtree
    return this._points;
  }
}

Is there a way to speed this up?有没有办法加快这个速度?

I would think that this kind of problem is quite common when working with nested/treelike datastructures but I have not found a satisfying solution.我认为这种问题在使用嵌套/树状数据结构时很常见,但我还没有找到令人满意的解决方案。

The only thing that I can think of is to replace your accessor property with a function (or perhaps add the function alongside it) and pass the target array in so that it can be filled in directly.我能想到的唯一一件事就是用 function 替换您的访问器属性(或者可能在其旁边添加 function)并传递目标数组以便可以直接填充它。 Something like:就像是:

class QuadTree {

  // ...
  // some other methods
  // ...

  getDividedPoints(target = []) {
    this.subtrees[0].getDividedPoints(target);
    this.subtrees[1].getDivdedPoints(target);
    this.subtrees[2].getDivdedPoints(target);
    this.subtrees[3].getDivdedPoints(target);
    return target;
  }

  get points() {
    if (this.divided) {
      return this.getDividedPoints([]);
    }
    // return _points array of this quadtree
    return this._points;
  }
}

Or if subtrees is a normal array, getDividedPoints could be:或者如果subtrees是一个普通数组, getDividedPoints可以是:

getDividedPoints(target = []) {
  this.subtrees.forEach(subtree => subtree.getDividedPoints(target));
  return target;
}

Otherwise, since I assume you want to copy this.subtrees[0].points (rather than inserting into it), concat 's going to be pretty good.否则,由于我假设您要复制this.subtrees[0].points (而不是插入其中),因此concat会非常好。

If the subtrees may or may not be divided, you'll need the if in the function.如果子树可能会或可能不会被划分,您将需要 function 中的if And I just belatedly noticed the name of the class.而我才刚刚注意到 class 的名称。 :-) So I'd probably just make the four calls: :-) 所以我可能只打四个电话:

getPoints(target) {
  if (this.divided) {
    target = target || [];
    this.subtrees[0].getPoints(target);
    this.subtrees[1].getPoints(target);
    this.subtrees[2].getPoints(target);
    this.subtrees[3].getPoints(target);
    return target;
  }
  if (target) {
    target.push.apply(target, this._points);
    return target;
  }
  return this._points; // <== If you're really okay with giving this
                       // to the caller (you were in your original code)
}

get points() {
  return this.getDividedPoints([]);
}

...or any of several other spins on that basic idea. ...或任何其他关于该基本想法的旋转。

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

相关问题 调用 javascript function 的最快方法是什么 - What's the fastest way to call a javascript function 用Javascript中的另一个数组替换数组的一部分的最快方法是什么? - What is the fastest way to replace a section of an array with another array in Javascript? 在 JavaScript 中将 N 个未排序数组合并为 1 个排序数组的最快方法是什么? - What is the fastest way to merge N unsorted arrays into 1 sorted array in JavaScript? 用来自JavaScript数组的数据创建HTML表的最快方法是什么? - What is the fastest way to create HTML table with data from JavaScript array? 在 JavaScript 中遍历数组的最快方法是什么? - What's the fastest way to loop through an array in JavaScript? 在 JavaScript 中对大型(ish)数字数组进行排序的最快方法是什么 - What is the fastest way to sort a large(ish) array of numbers in JavaScript 在Javascript中重复数组的最快方法 - Fastest way to repeat an array in Javascript Javascript 递归函数。 将数据存储在数组中时结果错误 - Javascript Recursive function. Wrong results while storing data in array Javascript-使用纯递归函数创建具有FizzBu​​zz结果的数组 - Javascript - Creating an array with FizzBuzz results using a pure recursive function 在 JavaScript 中比较字符串的最快方法是什么? - What is the fastest way to compare strings in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM