JSFiddle - React, Tailwind, and code Playground

by Ryan Duffy

JavaScript

var a = [1,2,3];
var b = [4,5,6];
var c = [1,3,5,7];

union();
perf();

fixUnion();
union();
perf();

function perf() {
    var t = enyo.now();
    for(var i=0;i<100000;i++) {
        enyo.union(a,b,c);
    }
    
    console.log("100k run time: ", enyo.now()-t);
}

function union() {
    var through7 = enyo.union(a,b,c);
    var no2 = enyo.union(b,c);
    var no4or6 = enyo.union(a,c);
    var no7 = enyo.union(a,b);
    
    console.log("has 7 elements:", enyo.json.stringify(through7));
    console.log("no 2:", enyo.json.stringify(no2));
    console.log("no 4 or 6:", enyo.json.stringify(no4or6));
    console.log("no 7:", enyo.json.stringify(no7));
}


function fixUnion() {
    enyo.union = function (/* _arrays_ */) {
        // create one large array of all of the arrays passed to
        // the method for comparison
        var values = Array.prototype.concat.apply([], arguments);
        
        // the array of values actually to be returned
        var ret = [];
        
        // iterate through the provided values
        enyo.forEach(values, function(v) {
            // if not found in the list of values to return
            if(!~enyo.indexOf(v, ret)) {
                // add it
                ret.push(v);
            }
        });
        
        // and return the unique list
        return ret;
    };
}