sortBy vs sortBy-deep

Benchmark created by Enzo on


Setup

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} }
    ];

Test runner

Ready to run.

Testing in
TestOps/sec
sortBy
sortBy(data.slice(0), 'name', '-age', 'id' );
ready
sortBydeep
sortBydeep(data.slice(0), 'name', '-age', 'id' );
ready

Revisions

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