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 sortBy = Array.prototype.sortBy = function() {
var asc, property, i=arguments.length-1, until=1, array=arguments[0], one, two;
// If is called as method of the array itself
if (this instanceof Array) {
array = this;
until = 0;
}
for (; i>=until; i-- ) {
asc = 1;
property = arguments[i];
if ( property.charAt(0) == '-' && !array[0].hasOwnProperty(property) ) {
asc = -1;
property = property.slice(1);
}
array.sort(function( a, b ) {
one = one;
two = two;
if ( one === two )
return 0;
type = typeof one;
if ( type == 'string' )
return ((one > two) ? 1 : -1) * asc;
if ( type == 'number' || type == 'boolean' || one instanceof Date )
return (one - two) * asc;
else
return 1;
});
}
return array;
};
var sortBydeep = Array.prototype.sortBy = (function(){
var get = function(obj, path) {
try {
for (var i = 0; i<path.length-1; i++)
obj = obj[ path[i] ];
return obj[ path[i] ];
} catch(e) {
return obj;
}
};
return function() {
var asc, property, isdeep, one, two, path, i=arguments.length-1, until=1, array=arguments[0];
// If is called as method of the array itself
if (this instanceof Array) {
array = this;
until = 0;
}
for (; i>=until; i-- ) {
asc = 1;
path = arguments[i].split('.');
property = path[0];
if ( property.charAt(0) == '-' && !array[0].hasOwnProperty(property) ) {
asc = -1;
property = path[0] = property.slice(1);
}
isdeep = path.length > 1;
array.sort(function( a, b ) {
if (isdeep) {
one = get(a, path);
two = get(b, path);
}
else {
one = a[property];
two = b[property];
}
if ( one === two )
return 0;
type = typeof one;
if ( type == 'string' )
return ((one > two) ? 1 : -1) * asc;
if ( type == 'number' || type == 'boolean' || one instanceof Date )
return (one - two) * asc;
else
return 1;
});
}
return array;
}
})();
var data = [
{id:4, name:"Josema", age:34, work: {isworking:true} },
{id:5, name:"Enzo", age:29, work: {isworking:true} },
{id:2, name:"Josema", age:29, work: {isworking:false} },
{id:1, name:"Enzo", age:29, work: {isworking:false} },
{id:3, name:"Enzo", age:34, work: {isworking:false} }
];
Ready to run.
Test | Ops/sec | |
---|---|---|
sortBy |
| ready |
sortBydeep |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.