Immutable.js withMutations vs merge

JavaScript performance comparison

Test case created by Andy Edwards

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.1/immutable.min.js"></script>
 
<script>
  var obj = {};
  for(var i=0; i<10000; i++) {
    obj[i] = i;
  }

  var emptyBaseMap = Immutable.Map();
  var complexBaseMap = Immutable.fromJS(obj);

  var testWithMutations = function (baseMap, numUpdates) {
    var newMap = baseMap.withMutations(function (newMap) {
      for(i=0; i<numUpdates; i++) {
        newMap.update(String(i), function (i) { return i + 1; });
      }
    });    
  };

  var testMerge = function (baseMap, numUpdates) {
    var updates = {};
    for(i=0; i<numUpdates; i++) {
      updates[i] = baseMap.get(String(i)) + 1;
    }
    var newMap = baseMap.merge(updates);
  };
</script>
    
    

Preparation code output

<script> var obj = {}; for(var i=0; i<10000; i++) { obj[i] = i; } var emptyBaseMap = Immutable.Map(); var complexBaseMap = Immutable.fromJS(obj); var testWithMutations = function (baseMap, numUpdates) { var newMap = baseMap.withMutations(function (newMap) { for(i=0; i<numUpdates; i++) { newMap.update(String(i), function (i) { return i + 1; }); } }); }; var testMerge = function (baseMap, numUpdates) { var updates = {}; for(i=0; i<numUpdates; i++) { updates[i] = baseMap.get(String(i)) + 1; } var newMap = baseMap.merge(updates); }; </script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Empty map, 10 updates: withMutations
testWithMutations(emptyBaseMap, 10);
pending…
Empty map, 10 updates: merge
testMerge(emptyBaseMap, 10);
pending…
Complex map, 10 updates: withMutations
testWithMutations(complexBaseMap, 10);
pending…
Complex map, 10 updates: merge
testMerge(complexBaseMap, 10);
pending…
Empty map, 100 updates: withMutations
testWithMutations(emptyBaseMap, 100);
pending…
Empty map, 100 updates: merge
testMerge(emptyBaseMap, 100);
pending…
Complex map, 100 updates: withMutations
testWithMutations(complexBaseMap, 100);
pending…
Complex map, 100 updates: merge
testMerge(complexBaseMap, 100);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments