Array resize strategies

Benchmark created on


Setup

function resizeFill(x, n, vd) {
  var X = x.length; x.length = n;
  if (n>X) x.fill(vd, X);
}

function resizeLoop(x, n, vd) {
  var X = x.length;
  for (var i=X; i<n; ++i)
    x[i] = vd;
  x.length = n;
}

var x = [];
for (var i=0; i<1000; ++i)
  x[i] = Math.random();

Test runner

Ready to run.

Testing in
TestOps/sec
Resize using fill()
var y = x.slice();
for (var i=0; i<100; ++i) {
  var n = Math.round(x.length * Math.random());
  resizeFill(y, n);
}
ready
Resize using loop
var y = x.slice();
for (var i=0; i<100; ++i) {
  var n = Math.round(x.length * Math.random());
  resizeLoop(y, n);
}
ready

Revisions

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