Array chunking variable

JavaScript performance comparison

Test case created by Blake Kus

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  /*
  Given an array, chunk every 5 items, and put the first item of every chunk as its own entry and group the remainder.
  
  Example:
  Input: [1,2,3,4,5,6,7,8,9,10,11,12]
  Output: [1, [2,3,4,5], 6, [7,8,9,10], 11, [12]]
  */
  
  let items = [1,2,3,4,5,6,7,8,9,10,11,12];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for
// Blake
let formattedItems = [];
let group = [];
for (var i = 0; i < items.length; i++) {
  let item = items[i];
  if (i % 5 === 0) {
    if (group.length) {
      formattedItems.push(group);
      group = [];
    }
    formattedItems.push(item);
  } else {
    group.push(item);
  }
}
if (group.length) {
  formattedItems.push(group);
}
pending…
map
// Zac
let formattedItems = [];
formattedItems = items.map(( item, i ) => {
  return i % 5 === 0 ? item : i % 5 === 1 ? items.slice(i,i+4) : null;
}).filter( item => item );
pending…
stupid
// Blake
var formattedItems = Array.apply(null, Array((Math.floor(items.length/5) * 2) + (items.length/5 % 1 === 0 ? 0 : (+(items.length/5 % 1).toFixed(2) === 1/5 ? 1 : 2)))).map(function (v, k) {
  return k % 2 === 0 ? items[k/2 * 5] : items.slice((Math.floor(k/2) * 5) + 1, (Math.floor(k/2) * 5) + 5);
});
pending…
dur
// Zac
let formattedItems = [];
let split = Math.ceil( items.length / 5 );
for (let i = 0; i < split; i++) {
  let chunk1 = items[i*5];
  let chunk2 = items.slice((i*5)+1, (i*5)+5);
  formattedItems.push( chunk1 );
  if ( chunk2.length ) formattedItems.push( chunk2 );
}
pending…
Array.forEach
// Josh
let formattedItems = [];
let group = [];
items.forEach((item, i) => {
  if (i % 5 === 0) {
    if (group.length) {
      formattedItems.push(group);
      group = [];
    }
    formattedItems.push(item);
  } else {
    group.push(item);
  }
});
if (group.length) {
  formattedItems.push(group);
}
pending…
while
// Blake
var formattedItems = [];
var group = [];
var j = l = items.length;
var i;
while(j--) {
  var i = l-j-1;
  if (i % 5 === 0) {
    if (group.length) {
      formattedItems.push(group);
      group = [];
    }
    formattedItems.push(items[i]);
  } else {
    group.push(items[i]);
  }
}
if (group.length) {
  formattedItems.push(group);
}
pending…
stupid v2
// Blake
var formattedItems = new Array((Math.floor(items.length/5) * 2) + (items.length/5 % 1 === 0 ? 0 : (+(items.length/5 % 1).toFixed(2) === 1/5 ? 1 : 2)));
for (var i = 0; i < formattedItems.length; i++) {
   if (i % 2 === 0) {
    formattedItems[i] = items[i/2 * 5];
  } else {
    formattedItems[i] = items.slice((Math.floor(i/2) * 5) + 1, (Math.floor(i/2) * 5) + 5);
  }
}
pending…
dur v2
// Zac
var formattedItems = [];
let split = Math.ceil( items.length / 5 );
for (let i = 0; i < split; i++) {
  let pos = i*5;
  let chunk = [];
  for (let c = pos+1; c < pos+5; c++ ) {
    items[c] ? chunk.push(items[c]) : null;
  }
  formattedItems.push( items[pos] );
  if ( chunk.length ) formattedItems.push( chunk );
}
pending…
dur v3
// Zac
let formattedItems = [];
let split = Math.ceil( items.length / 5 );
for (let i = 0; i < split; i++) {
  let pos = i*5;
  let chunk = items.filter((item, j) => j >= pos+1 && j <= pos+4);
  formattedItems.push(items[pos]);
  if ( chunk.length ) formattedItems.push(chunk);
}
pending…
stupid v3
// Blake
var formattedItems = new Array((Math.floor(items.length/5) * 2) + (items.length/5 % 1 === 0 ? 0 : (+(items.length/5 % 1).toFixed(2) === 1/5 ? 1 : 2)));
var group = [];
var offset;
for (var i = 0; i < formattedItems.length; i++) {
   if (i % 2 === 0) {
    formattedItems[i] = items[i/2 * 5];
  } else {
    offset = (Math.floor(i/2) * 5) + 1;
    group = new Array(Math.min(4, items.length - offset));
    for (var j = 0; j < group.length; j++) {
      group[j] = items[offset + j];
    }
    formattedItems[i] = group;
  }
}
pending…
reverse for
// Aaron
var formattedItems = [];
var group = [];

for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];

    if (i % 5 === 0) {
        if (group.length) {
            formattedItems.unshift(group);
            group = [];
        }
        
        formattedItems.unshift(item);
    }

    else {
        group.unshift(item);
    }
}

if (group.length) {
    formattedItems.unshift(group);
}
pending…
reverse for no reverse array
var formattedItems = [];
var group = [];
var l = items.length - 1;
for (var i = l; i >= 0; i--) {
    if ((l - i) % 5 === 0) {
        if (group.length) {
            formattedItems.push(group);
            group = [];
        }
        formattedItems.push(items[l - i]);
    } else {
        group.push(items[l - i]);
    }
}
if (group.length) {
    formattedItems.push(group);
}
pending…
reverse for reverse array
// Blake
var formattedItems = [];
var group = [];
for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];
    if (i % 5 === 0) {
        if (group.length) {
            formattedItems.push(group.reverse());
            group = [];
        }
        formattedItems.push(item);
    } else {
        group.push(item);
    }
}
if (group.length) {
    formattedItems.push(group);
}
formattedItems.reverse();
pending…
reverse for no reverse array
var formattedItems = [];
var group = [];
var l = items.length - 1;
for (var i = l; i >= 0; i--) {
    if ((l - i) % 5 === 0) {
        if (group.length) {
            formattedItems.push(group);
            group = [];
        }
        formattedItems.push(items[l - i]);
    } else {
        group.push(items[l - i]);
    }
}
if (group.length) {
    formattedItems.push(group);
}
pending…

Revisions

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

0 Comments