build a Xbonacci function that takes a signature of X elements ,each next element is the sum of the last X elements ,and returns the first n elements this is a kata from codewars, here is the description of the kata:
think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well Cinquebonacci would probably sound a bit more Italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on.
Well, guess what? You have to build an Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.
and this is my solution to it:
function Xbonacci(signature,n){
 let i=0;
  let k = n - signature.length;
   while(k--){
     let sumNums = 0;
     //let newArray = [...signature];
     signature.slice(i , signature.length ).map((num)=&gt;{
      return sumNums += num;
     })
     
     signature.push(sumNums);
     i++
   }
  return signature;
}

the code works well but it doesn't pass the test because of optimization. Is there any way to make this code faster or more optimized? I think the problem is the slice method but I don't what to use it instead.
I think the problem is the slice method but I don't what to use it instead.

It is, but it's not as simple as taking out the slice and using something else in its place, the problem is more fundamental than that.
This algorithm scales poorly to high values of the original length of signature, by the way let's call this value S so I don't have keep spelling it all out. There is an other approach that does not depend on S, by updating the &quot;sum of past S values&quot; in constant time. Instead of explicitly taking the window of S elements and summing it, the sum of the previous window can be updated: one new value enters the window (added to the sum), and one old value leaves the window (subtracted from the sum). The work done for each element of the output is only one addition and one subtraction, not S additions.