Xbonacci perf test

Benchmark created on


Setup

function xbonacci_fast(signature, maxLength) {
  if (maxLength < signature.length) return signature.slice(0, maxLength);
  
  const length = signature.length;
  let initialSum = signature.reduce((acc, cur) => acc + cur, 0);
  const res = [...signature, initialSum];
  initialSum -= res[0]; // remove first element to prepare for the loop

  for (let i = length; i < maxLength - 1; i++) {
    initialSum += res[res.length - 1];
    res.push(initialSum);
    initialSum -= res[res.length - length - 1];
  }

  return res;
}

function xbonacci_slow(signature, n){
  const x = signature.length;
  const arr = [...signature];
  for(let i = 0; i < n-x; i++) {
    arr.push(arr.slice(i, i+x).reduce((p,c)=>p+c));
  }
  return arr.slice(0,n);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Performant version
xbonacci_fast([1,1,1,1,1], 1000);
ready
"Tidy" version
xbonacci_slow([1,1,1,1,1], 1000);
ready

Revisions

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