tt

Benchmark created on


Setup

/*
        
           ##    #####      Copyright (c) - Kevin McGinty
         # _ #  ###        
        #   #  #            AtomicFrameworks
        
    */
    /*jslint plusplus: true */
    (function(globals){
        "use strict";
        var mappers;
        globals.mappers = mappers = {
            obj: function (collection, callback, thisArg) {
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                var key, ret = {};
                for (key in collection) {
                    if (collection.hasOwnProperty(key)) {
                        ret[key] = callback.call(thisArg, collection[key], key, collection);
                    }
                }
                return ret;
            },
            arr: function (collection, callback, thisArg) {
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                var i = 0, len = collection.length, ret = [];
                while (i < len) {
                    ret[i] = callback.call(thisArg, collection[i], i, collection);
                    ++i;
                }
                return ret;
            },
            deep: function (collection, callback, thisArg) {
                var ret, i, len;
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                // Map object by key
                if (collection.toString() === '[object Object]') {
                    ret = {};
                    for (i in collection) {
                        if (collection.hasOwnProperty(i)) {
                            // Recurse any objects (this includes arrays)
                            if (typeof collection[i] === "object") {
                                ret[i] = mappers.deep(collection[i], callback, thisArg);
                            } else {
                                ret[i] = callback.call(thisArg, collection[i], i, collection);
                            }
                        }
                    }
                } else {
                    // Else map string and array to array by index
                    // IE 7 cannot iterate over strings
                    if (typeof collection === "string") {
                        collection = collection.split('');
                    }
                    i = 0;
                    len = collection.length;
                    ret = [];
                    while (i < len) {
    
                        if (typeof collection[i] === "object") {
                            ret[i] = mappers.deep(collection[i], callback, thisArg);
                        } else {
                            ret[i] = callback.call(thisArg, collection[i], i, collection);
                        }
                        ++i;
                    }
                }
                return ret;
            }
        };
        // Extend array with map if not native
        // Syntax - array.map(callback[, thisArg]);
        if (!Array.prototype.map) {
            Array.prototype.map = function (callback, thisArg) {
                return mappers.arr(this, callback, thisArg);
            };
        }
        // Extend object with map if not native
        // Syntax - array.map(callback[, thisArg]);
        if (!Object.prototype.map) {
            Object.prototype.map = function (callback, thisArg) {
                return mappers.obj(this, callback, thisArg);
            };
        }
        // Extend Object with mapDeep
        // Syntax - array.map(callback[, thisArg]);
        if (!Object.prototype.mapDeep) {
            Object.prototype.mapDeep = function (callback, thisArg) {
                return mappers.deep(this, callback, thisArg);
            };
        }
        // Extend Array with mapDeep
        // Syntax - array.map(callback[, thisArg]);
        if (!Array.prototype.mapDeep) {
            Array.prototype.mapDeep = function (callback, thisArg) {
                return mappers.deep(this, callback, thisArg);
            };
        }
    }(this));
    
    
    
    /*
        
           ##    #####      Copyright (c) - Kevin McGinty
         # _ #  ###        
        #   #  #            AtomicFrameworks
        
    */
    /*jslint plusplus: true */
    (function(globals){
        "use strict";
        globals.mappersTwo = {
            obj: function (collection, callback, thisArg) {
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                var key, ret = {};
                for (key in collection) {
                    if (collection.hasOwnProperty(key)) {
                        ret[key] = callback.call(thisArg, collection[key], key, collection);
                    }
                }
                return ret;
            },
            arr: function (collection, callback, thisArg) {
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                var i = 0, len = collection.length, ret = [];
                while (i < len) {
                    ret[i] = callback.call(thisArg, collection[i], i, collection);
                    ++i;
                }
                return ret;
            },
            deep: function (collection, callback, thisArg) {
                var ret, i, len;
                if (typeof callback !== "function") {
                    throw new TypeError(callback + " is not a function");
                }
                // Map object by key
                if (collection.toString() === '[object Object]') {
                    ret = {};
                    for (i in collection) {
                        if (collection.hasOwnProperty(i)) {
                            // Recurse any objects (this includes arrays)
                            if (typeof collection[i] === "object") {
                                ret[i] = globals.mappersTwo.deep(collection[i], callback, thisArg);
                            } else {
                                ret[i] = callback.call(thisArg, collection[i], i, collection);
                            }
                        }
                    }
                } else {
                    // Else map string and array to array by index
                    // IE 7 cannot iterate over strings
                    if (typeof collection === "string") {
                        collection = collection.split('');
                    }
                    i = 0;
                    len = collection.length;
                    ret = [];
                    while (i < len) {
    
                        if (typeof collection[i] === "object") {
                            ret[i] = globals.mappersTwo.deep(collection[i], callback, thisArg);
                        } else {
                            ret[i] = callback.call(thisArg, collection[i], i, collection);
                        }
                        ++i;
                    }
                }
                return ret;
            }
        };
        // Extend array with map if not native
        // Syntax - array.map(callback[, thisArg]);
        if (!Array.prototype.map) {
            Array.prototype.map = function (callback, thisArg) {
                return globals.mappers.arr(this, callback, thisArg);
            };
        }
        // Extend object with map if not native
        // Syntax - array.map(callback[, thisArg]);
        if (!Object.prototype.map) {
            Object.prototype.map = function (callback, thisArg) {
                return globals.mappers.obj(this, callback, thisArg);
            };
        }
        // Extend Object with mapDeep
        // Syntax - array.map(callback[, thisArg]);
        if (!Object.prototype.mapDeepTwo) {
            Object.prototype.mapDeepTwo = function (callback, thisArg) {
                return globals.mappersTwo.deep(this, callback, thisArg);
            };
        }
        // Extend Array with mapDeep
        // Syntax - array.map(callback[, thisArg]);
        if (!Array.prototype.mapDeep) {
            Array.prototype.mapDeep = function (callback, thisArg) {
                return globals.mappers.deep(this, callback, thisArg);
            };
        }
    }(this));

Test runner

Ready to run.

Testing in
TestOps/sec
1
var a = [], b;
for (var i = 0; i < 10000; i++) {
  a[i] = [500];
}
var b = a.mapDeep(function(x){return x});
ready
2
var a = [], b;
for (var i = 0; i < 10000; i++) {
  a[i] = [500];
}
var b = a.mapDeep(function(x){return x});
ready

Revisions

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