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
Various methods for finding the set that is the relative complement of "buildings" in "constructions"
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
var constructions = [{
Name: "Mess hall",
SqFt: 5000
}, {
Name: "Infirmary",
SqFt: 2000
}, {
Name: "Bungalow",
SqFt: 2000
}, {
Name: "HQ",
SqFt: 2000
}];
var buildings = [{
Name: "Infirmary",
SqFt: 2000
}, {
Name: "HQ",
SqFt: 2000
}];
function getValues(arr, key) {
var out = [];
for (var i = 0, l = arr.length; i < l; i++) {
out.push(arr[i][key]);
}
return out;
}
function notFoundIn(arr, arr2) {
// grab the names of the buildings
var buildings = getValues(arr2, 'Name');
// grab the names from the construction objects and filter
// those that are not in the building array
return getValues(arr, 'Name').filter(function (el) {
return buildings.indexOf(el) === -1;
});
}
if (!Array.prototype.notFoundIn) {
Array.prototype.notFoundIn = function (inThisArray, key) {
var thisArr = key ? getValues(this, key) : this;
var arrIn = key ? getValues(inThisArray, key) : inThisArray;
var out = [];
for (var i = 0, l = thisArr.length; i < l; i++) {
if (arrIn.indexOf(thisArr[i]) === -1) {
out.push(thisArr[i]);
}
}
return out;
}
}
// jquery grep functionized
if (!Array.prototype.subtractSet) {
Array.prototype.subtractSet = function (set, key) {
return $.grep(this, function (a) {
return $.grep(set, function (b) {
return key ? b[key] === a[key] : a === b;
}).length == 0;
});
}
}
Ready to run.
Test | Ops/sec | |
---|---|---|
nested jQuery.grep() for relative complement of "buildings" in "constructions" |
| ready |
mapped index lookup with array.filter() |
| ready |
extending Array with set relative complement function |
| ready |
jQuery.grep() set difference |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.