Immutable Union (v5)

Revision 5 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

function getListA() {
      var i = 0;
      var list = [];
      while (++i < 1000) {
        list.push(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();

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 (!l.includes(x)) {
      l.push(x);
    }
  });
  
  return Immutable.List(l);
}

var c = union(a,b);
 
ready
lodash union
function union(left,right){
  var l = left.toJS();
  
  var result = _.union(left, right);
  
  return Immutable.List(result);
}

var c = union(a,b);
 
ready
toSet
a.toOrderedSet().merge(b).toList();
ready
Array includes 2
function union(left,right){
  var l = left.toJS();
  var count = right.length;

  for (var i = 0; i < count; i++) {
    if (!l.includes(right[i])) {
      l.push(right[i]);
    }
  }
  
  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.