title: Is it inefficient to use a large number of closures?
body: <p>My webapp has an undo/redo concept.  I am considering how best to implement it.</p>
  
  <p>My current approach is to have an array of arrays where each array is a bunch of callbacks to invoke to restore that version.  E.g. for a label to restore its old value would be:</p>
  
  <pre><code>var undoer = {
      at = 0,
      stack = [[]],
      push = function(do,undo) {
          if(undoer.at &gt; 0) {
              undoer.splice(0,undoer.at,[]);
              undoer.at = 0;
          }
          undoer.stack[0].push([do,undo]);
      },
      commit = function() { 
          if(undoer.at == 0 &amp;&amp; undoer.stack[0])
              stack.unshift([]);
      },
      rollback = function() {
          if(undoer.at == 0 &amp;&amp; undoer.stack[0])
              while(var func: stack[0].pop())
                  func[1]();
      },
      undo = function() {
          // move the at index back, call all the undos
      ...
      redo = function() {
          // move the at index forward, call al dos
      ...
  };
  
  function Label(text) {
      var _text = text,
          label = {
              get: function() { return _text; },
              set: function(text) {
                  var old = text;
                  _text = text;
                  undoer.push(
                      function() { _text = text; }, // do
                      function() { _text = old; } // undo
                  );
              },
              ...
      };
      return label;
  }
  </code></pre>
  
  <p>(apologies for typos and omissions in this pseudocode)</p>
  
  <p>This is storing the state changes needed to undo and redo a change in closures.</p>
  
  <p>Imagine now many thousands of edits.  Would it be more memory efficient to instead use variadic functions and put copies of the <code>arguments</code> into the undo stack?</p>
  
  <p>And are there better ways to do undo/redo?</p>