Generate all partitions of a set of items Here are the items:

    var item = ['a', 'b', 'c', 'd'];


Desired output:

    [
        [ ['a'],['b'],['c'],['d'] ],
        [ ['a'],['b','c'],['d'] ],
        [ ['a'],['b'],['c','d'] ],
        [ ['a'],['b','c','d'] ],
        [ ['a'],['c'],['b','d'] ],....
    ]


The code:


function getCombinations(items) {
   var res = [];
   for (var i = 0; i &lt; items.length; i++) {
      for (var j = i + 1; j &lt;= items.length; j++) {
         var temp = items.slice(i, j);
         var remained = items.slice(0, i).concat(items.slice(j, items.length));
         if (remained.length &gt; 0) {
            var combinations = getCombinations(remained);
            combinations.forEach(combination =&gt; {
               res.push([temp].concat(combination));
            });
         } else {
            res.push([temp]);
         }
      }
    }
   return res;
}

var item = ['a', 'b', 'c', 'd'];

console.log(getCombinations(item));
I see the performance tag added to this post. Did you intent to have answers address performance in terms of time, space or other aspects?
If you are looking to optimize performance in terms of time then I must restrain myself from suggesting changing the outer for loop to a for of loop. However I do see there is a forEach loop inside the nested for loop. If you really want to optimize for performance, then consider using a for loop instead of that .forEach method. Also consider whether the same result can be achieved by iterating backwards through the loops - this would allow for fewer operations1.
Given that an arrow function is used in the .forEach() callback, other ecmascript-6 features can be used - e.g. const and let instead of var, spread syntax instead of calling .concat(), etc.