Immutable Union (v6)

Revision 6 of this benchmark created on


Description

Comparing solutions offered at http://stackoverflow.com/questions/30126698/how-to-get-union-of-several-immutable-js-lists

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.6/immutable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>

Setup

var ids = Immutable.Map();
    
    function getListA() {
      var i = 0;
      var list = [];
      while (++i < 1000) {
        list.push(i);
        ids = ids.set(i,i);
      }
      return list;
    }
    
    function getListB() {
      var i = 990;
      var list = [];
      while (++i < 1010) {
        list.push(i);
      }
      return list;
    }
    
    var a = Immutable.List(getListA());
    var b = getListB();
    var s = Immutable.OrderedSet(getListA());

Test runner

Ready to run.

Testing in
TestOps/sec
Union ala Travis J
function union(left,right){
 var union = new Map();
 left.forEach(function(x){
    union.set(x, x);
  });
  right.forEach(function(x){
    union.set(x, x);
  });
 return Immutable.List(union.values());
}

var c = union(a,b);
 
ready
Array includes
function union(left,right){
  var l = left.toJS();
  
  right.forEach(function(x){
    if (!ids.has(x)) {
      l.push(x);
      ids = ids.set(x, x);
    }
  });
  
  return Immutable.List(l);
}

var c = union(a,b);
 
ready
Array includes 2
function union(left,right){
  var l = left.toJS();
  
  right.forEach(function(x){
    if (!l.includes(x)) {
      l.push(x);
    }
  });
  
  return Immutable.List(l);
}

var c = union(a,b);
ready

Revisions

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