javascript 数组的快速分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6781722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:51:00  来源:igfitidea点击:

Fast grouping of a javascript array

javascriptarrays

提问by Joe

I have an array of a couple thousand strings

我有几千个字符串的数组

['7/21/2011', '7/21/2011', '7/21/2011', '7/20/2011', etc]

I am currently, running this code to group by the string and get the max group value:

我目前正在运行此代码以按字符串分组并获取最大组值:

var max = 0;
var group = {};
arr.map(function (value) {
  if (group[value]) {
    group[value]++;
  } else {
    group[value] = 1;
  }
  max = Math.max(max, group[value]);
});

Are there any improvements to make this code run faster?

是否有任何改进可以使此代码运行得更快?

EDIT:The results are in: http://jsperf.com/javascript-array-grouping2

编辑:结果在:http: //jsperf.com/javascript-array-grouping2

EDIT EDIT: that test was flawed. Mike Samuel's code was the fastest.

编辑 编辑:该测试有缺陷。Mike Samuel的代码是最快的。

6000 entries test -> http://jsperf.com/javascript-array-grouping2

6000 个条目测试 -> http://jsperf.com/javascript-array-grouping2

10K entries test -> http://jsperf.com/javascript-array-grouping

10K 条目测试 -> http://jsperf.com/javascript-array-grouping

采纳答案by Mike Samuel

If you're sure this is a hotspot and speed is really important, I would try to cut out several thousand function calls by inlining maxand map.

如果您确定这是一个热点并且速度非常重要,我会尝试通过内联maxmap.

You can also make the body of your function faster by cutting out a comparison.

您还可以通过删除比较来使函数体更快。

var max = 0;
var group = {};
for (var i = arr.length; --i >= 0;) {
  var value = arr[i];
  var n = group[value] = 1 - -(group[value] | 0);
  if (n > max) { max = n; }
}

The best thing to do is measure on the browsers you care about.

最好的办法是衡量您关心的浏览器。

回答by orlp

Yes certainly. I would calculate the max last, instead of every iteration, and not use an if:

是的,当然了。我会最后计算最大值,而不是每次迭代,并且不使用 if:

var group = {};
arr.map(function (value) {
    group[value] = (group[value] || 0) + 1;
});

var max = 0;
for (key in group) {
    if (group[key] > max) max = group[key];
}

EDIT: As Mike Samuel says you mightget faster by using an index instead of map:

编辑:正如 Mike Samuel 所说,使用索引而不是地图可能会更快:

var group = {};
var max = 0;

for (var i = arr.length; --i >= 0;) {
    group[value] = (group[value] || 0) + 1;
}
for (key in group) {
    if (group[key] > max) max = group[key];
}

回答by 6502

I think it really depends on the JS engine that you will run this code on. An alternative I think it's worth a shot is using

我认为这真的取决于您将运行此代码的 JS 引擎。我认为值得一试的另一种选择是使用

n = group[value] = (group[value]||0) + 1;
if (n > max) max = n;

for each element.

对于每个元素。

I also think that may be using a regular loop can be faster because the variables you will use will be just locals and not closed-over variables of a closure (that are normally slower) and you will also save a function call per element. Both those problems are non-issues if the implementation can inline this closure, but I don't know if there are JS implementations smart enough for that.

我还认为使用常规循环可能会更快,因为您将使用的变量只是局部变量,而不是闭包的封闭变量(通常较慢),并且您还将为每个元素保存一个函数调用。如果实现可以内联这个闭包,那么这两个问题都不是问题,但我不知道是否有足够智能的 JS 实现。