jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
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;
});
};
Ready to run.
Test | Ops/sec | |
---|---|---|
old commonApps |
| ready |
new commonApps |
| ready |
sorted commonApps |
| ready |
newIdentifyCommonApps using Array.intersection |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.