Javascript Array Grouping (v7)

Revision 7 of this benchmark created by Andrey Gershun on


Description

different methods for grouping string in an array and getting the max value of the grouping.

Preparation HTML

<script src="http://alasql.org/console/alasql.min.js"></script>
<script>
  var arr = [];
  var day, month, year;
  for (var i = 0; i < 10000; i++) {
    day = Math.round(Math.random() * 30);
    month = Math.round(Math.random() * 12);
    year = Math.round(Math.random() * 3 + 2009);
    arr.push(month + "/" + day + "/" + year);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
nightcracker
var group = {};
var max = 0;
var value;
for (var i = arr.length; --i >= 0;) {
  value = arr[i];
  group[value] = (group[value] | 0) + 1;
}
for (key in group) {
  if (group[key] > max) max = group[key];
}
ready
Mike Samuel
var max = 0;
var group = {};
var value, n, len = arr.length;
for (var i = len; --i >= 0;) {
  value = arr[i];
  n = group[value] = 1 - -(group[value] | 0);
  if (n > max) {
    max = n;
  }
}
ready
Mike Samuel edited
var max = 0,
  group = {},
  value, n, len = arr.length;
for (var i = len; --i >= 0;) {
  value = arr[i];
  n = group[value] = 1 - -(group[value] | 0);
  if (n > max) {
    max = n;
  }
}
ready
Alasql
var max = alasql('SELECT MAX(cnt) FROM (SELECT COUNT([0]) AS cnt FROM [?] GROUP BY [0])', [arr]);
ready
Alasql (without array conversion)
var max = alasql('SELECT MAX(cnt) FROM (SELECT _,COUNT(*) AS cnt FROM ? GROUP BY _)', [arr]);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.