# Immutable Union (v6)

## 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

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);
``````
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);
``````
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);``````