title: How would you calculate the space complexity of this function?
body: <p>I've been asked to calculate space complexity for an 'anagram machine'
  function I've created. The function returns all possible string combinations by replacing letters. And has a 'memoization' object to help in calculation performance. So the question is: How would you calculate the space complexity of this function?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const arrangeLetters = (function () {
      let dat = {
          memoization: {}
      };
      const arrange = (word) =&gt; {
          if (word.length &lt; 2) {
              dat.memoization[word] = [word];
              return [word]
          }
          let curLetter, restOfWord;
          let newEntry = '';
          let allAnswers = [];
          let index = 0;
          let anagrams = [];
          let i, j;
          for (i = 0; i &lt; word.length; i++) {
              curLetter = word[i];
              restOfWord = word.substr(0, i) + word.substr(i + 1, word.length - 1);
              let shortwordArray = dat.memoization[restOfWord] || arrange(restOfWord);
  
              for (j = 0; j &lt; shortwordArray.length; j++) {
                  newEntry = curLetter + shortwordArray[j];
                  anagrams[index++] = newEntry;
                  dat.memoization[curLetter + restOfWord] = anagrams;
                  allAnswers.push(newEntry);
              }
          }
          return allAnswers;
      };
      return arrange;
  })();
  
  console.log(arrangeLetters('abefghi'));</code></pre>
  </div>
  </div>
  </p>