Steamroll Array

Benchmark created on


Setup

const OUT_OF_BOUNDS = Symbol("OOB");

function steamrollArray(input) {
  const loc = [0];
  const steamrolled = [];

  while(loc[0] < input.length) {
    const val = loc.reduce(
      (v, i) => Array.isArray(v) && i < v.length ?
          v[i] : 
          OUT_OF_BOUNDS,
      input,
    );

    if (Array.isArray(val)) {
      loc.push(-1);
    } else if (val === OUT_OF_BOUNDS) {
      loc.pop();
    } else {
      steamrolled.push(val);
    }
    
    loc[loc.length - 1]++;
  }

  return steamrolled;
}

const steamrollArray2 = (array) => {
  let isFinished = array.every(el => !Array.isArray(el));
  while (!isFinished) {
    for (const index in array) {
      if (Array.isArray(array[index])) {
        array.splice(index, 1, ...array[index])
      }
    }
    isFinished = array.every(el => !Array.isArray(el))
  }
  return array;
}

Test runner

Ready to run.

Testing in
TestOps/sec
My solution
steamrollArray([{}, 2, [[[[5]]]], [0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, [[[[[[[[[[[[[[[[[[[[[[[[[[[[["hi"]]]]]]]]]]]]]]]]]]]]]]]]]]]]],0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,
false, undefined, null, 12]])
ready
Naomi's Cheater Solution
steamrollArray2([{}, 2, [[[[5]]]], [0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, [[[[[[[[[[[[[[[[[[[[[[[[[[[[["hi"]]]]]]]]]]]]]]]]]]]]]]]]]]]]],0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,
false, undefined, null, 12]])
ready

Revisions

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