Algorithm for dividing a number into largest "power of two" buckets?
CertainPerformance's solution using .toString(2) is clever, but for a fast solution to such an elementary problem, simple bit manipulation and a while loop is the way to go:


function split(number, bits = 5) {
  let unit = 1 &lt;&lt; bits;
  const result = new Array(number &gt;&gt; bits).fill(unit);
  while (unit &gt;= 1) {
    unit &gt;&gt;= 1;
    if (number &amp; unit) result.push(unit);
  }
  return result;
}

const numbers = [1, 2, 3, 4, 5, 6, 7, 30, 31, 32, 33, 20, 25, 36, 50, 100, 201];
numbers.forEach(n =&gt; console.log(n, '=', split(n).join(' + ')));



In particular, you can indeed avoid division and Math.floor() by using the bitwise right shift operator &gt;&gt; instead: n &gt;&gt; k is a fast and compact way of calculating Math.trunc(n / 2**k) for any integer n and any non-negative integer k.  Also, the bitwise AND operator &amp; makes it easy to check whether an integer has a particular bit set: n &amp; k evaluates to a non-zero value if and only if n and k have any set bits in common.

Ps. To make this code even faster, at the cost of some extra complexity, we can precalculate the length of the result array so that we don't need to use .push():


function split(number, bits = 5) {
  // precalculate the length of the result array
  const maxUnit = 1 &lt;&lt; bits, prefixLength = number &gt;&gt; bits;
  let length = prefixLength, unit = maxUnit;
  while (unit &gt;= 1) {
    unit &gt;&gt;= 1;
    if (number &amp; unit) length++;
  }

  // allocate and fill the array
  const result = new Array(length).fill(maxUnit);
  let i = prefixLength; unit = maxUnit;
  while (unit &gt;= 1) {
    unit &gt;&gt;= 1;
    if (number &amp; unit) result[i++] = unit;
  }
  return result;
}

const numbers = [1, 2, 3, 4, 5, 6, 7, 30, 31, 32, 33, 20, 25, 36, 50, 100, 201];
numbers.forEach(n =&gt; console.log(n, '=', split(n).join(' + ')));



According to a quick benchmark, the preallocating version is about 40% faster than the one using .push() (and both are several times faster than the other solutions posted so far).

Pps. It turns out that, maybe a bit counter-intuitively, just building the entire output array one element at a time using .push() may be the simplest and fastest solution, at least for inputs that aren't too huge.  The credit for this solution goes to superb rain, so I'll just link to their answer for it, but I've added it to my benchmark above.  It seems to perform about as well in the benchmark (for all numbers from 0 to 999) as my fastest solution on Firefox, and outperforms all my solutions on Chrome.