common-apps-test

Benchmark created on


Setup

var label1 = {
      links: {
        applications: []
      }
    },
      label2 = {
        links: {
          applications: []
        }
      };
    for (var i = 0; i < 500; i++) {
      label1.links.applications[i] = i * 2;
      label2.links.applications[i] = i * 3;
    }
    var selectedLabels = [label1, label2];
    
    var commonAppsIntersection = function(appliedLabels) {
      var common;
      if (appliedLabels.length === 1) {
        return appliedLabels[0].links.applications;
      } else if (appliedLabels.length > 0) {
        var sortedLabels = appliedLabels.sort(function(a, b) {
          return a.links.applications.length - b.links.applications.length;
        });
    
        common = sortedLabels[0].links.applications;
        for (var i = 1; i < sortedLabels.length; i++) {
          common = common.intersection(sortedLabels[i].links.applications);
          if (common.length === 0) {
            break;
          }
        }
        return common;
      }
    };
    
    var commonAppsIntersect = function(appliedLabels) {
      var applications = new Array(appliedLabels.length),
        commonApps = [];
      if (appliedLabels.length === 1) {
        return appliedLabels[0].links.applications;
      }
      if (appliedLabels.length > 0) {
        for (var i = 0; i < appliedLabels.length; i++) {
          applications[i] = appliedLabels[i].links.applications;
        }
        commonApps = intersect(applications);
      }
      return commonApps;
    };
    
    var commonAppsNewIntersect = function(appliedLabels) {
      var applications = new Array(appliedLabels.length);
      commonApps = [];
      if (appliedLabels.length === 1) {
        return appliedLabels[0].links.applications;
      }
      if (appliedLabels.length > 0) {
        for (var i = 0; i < appliedLabels.length; i++) {
          applications[i] = appliedLabels[i].links.applications;
        }
        commonApps = newIntersect(applications);
      }
      return commonApps;
    };
    
    var commonAppsSortedIntersect = function(appliedLabels) {
      var common;
      if (appliedLabels.length === 1) {
        return appliedLabels[0].links.applications;
      } else if (appliedLabels.length > 0) {
        var sortedLabels = appliedLabels.sort(function(a, b) {
          return a.links.applications.length - b.links.applications.length;
        });
    
        common = sortedLabels[0].links.applications;
        for (var i = 1; i < sortedLabels.length; i++) {
          common = common.sortedIntersection(sortedLabels[i].links.applications);
          if (common.length === 0) {
            break;
          }
        }
        return common;
      };
    };
    var intersect = function(arrays) {
      var arrays = sortFromSmallestToLargest(arrays),
        smallestArray = arrays[0],
        commonValues = [];
    
      for (var i = 0; i < smallestArray.length; i++) {
        var value = smallestArray[i];
        if (allHaveValue(arrays, value)) {
          commonValues.push(value);
        }
      }
      return commonValues;
    };
    
    var allHaveValue = function(arrays, value) {
      for (var i = 0; i < arrays.length; i++) {
        var arr = arrays[i],
          length = arr.length,
          allContainValue = false;
    
        for (var j = 0; j < length; j++) {
          if (arr[j] === value) {
            allContainValue = true;
            break;
          }
        }
    
        if (allContainValue === false) {
          return false;
        }
      }
    
      return true;
    };
    
    var newIntersect = function(arrays) {
      var arrays = sortFromSmallestToLargest(arrays),
        common = arrays[0];
    
      for (var i = 1; i < arrays.length; i++) {
        var a = arrays[i];
    
        common = common.intersection(a);
        if (common.length === 0) {
          break;
        }
      }
      return common;
    };
    
    
    Array.prototype.includes = function(b) {
      for (var i = 0, n = this.length; i < n; i++) {
        if (this[i] === b) {
          return true;
        }
      }
      return false;
    };
    
    Array.prototype.intersection = function(b) {
      var that = this;
      if (b.length > that.length) {
        that = b;
        b = this;
      }
      return that.filter(function(e) {
        return b.includes(e);
      });
    };
    
    Array.prototype.sortedIntersection = function(b) {
      var a = this,
        lastIndex = 0,
        result,
        shortLen, longLen, i, j, k;
      if (b.length > a.length) {
        a = b;
        b = this;
      }
      shortLen = b.length;
      longLen = a.length;
      result = Array(shortLen);
      k = 0;
      for (i = 0; i < shortLen; i++) {
        for (j = lastIndex; j < longLen; j++) {
          if (b[i] == a[j]) {
            result[k] = b[i];
            k++;
            lastIndex = j + 1;
            break;
          }
        }
      }
      return result.slice(0, k);
    };
    
    var sortFromSmallestToLargest = function(arrays) {
      return arrays.sort(function(a, b) {
        return a.length - b.length;
      });
    };

Test runner

Ready to run.

Testing in
TestOps/sec
old commonApps
commonAppsIntersect(selectedLabels);
ready
new commonApps
commonAppsNewIntersect(selectedLabels);
ready
sorted commonApps
commonAppsSortedIntersect(selectedLabels);
ready
newIdentifyCommonApps using Array.intersection
commonAppsIntersection(selectedLabels);
ready

Revisions

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