Homemade vs Native array functions

Benchmark created by Daniel Weiner on


Description

These work exactly the same as the native array functions, just without the type checking. Possibly because of the lack of type checking, these implementations are significantly faster than the native ones.

Setup

Array.prototype._forEach = function(cb, thisArg) {
                var len = this.length;
                for (var i = 0; i < len; i++) {
                        var result = cb.call(thisArg, this[i], i, this);
                        if (typeof result !== 'undefined' && !result) break;
                }
        }
    Array.prototype._forEachRight = function(cb, thisArg) {
        var len = this.length;
        for (var i = len - 1; i >= 0; i--) {
                var result = cb.call(thisArg, this[i], i, this);
                if (typeof result !== 'undefined' && !result) break;
        }
    }
    Array.prototype._map = function(cb, thisArg) {
        var arr = new Array(this.length);
        this._forEach(function(e, i, t) {
                arr[i] = cb(e, i, t);
                return true;
        }, thisArg);
        return arr;
    }
    Array.prototype._reduce = function(cb, initial) {
        this._forEach(function(e, i, t) {
                initial = cb(initial, e, i, t);
                return true;
        });
        return initial;
    }
    Array.prototype._reduceRight = function(cb, initial) {
        this._forEachRight(function(e, i, t) {
                initial = cb(initial, e, i, t);
                return true;
        });
    }
    Array.prototype._every = function(cb, thisArg) {
        var condition = true;
        this._forEach(function(e, i, t) {
                condition = !! cb(e, i, t);
                return condition;
        }, thisArg);
        return condition;
    }
    Array.prototype._some = function(cb, thisArg) {
        var condition = false;
        this._forEach(function(e, i, t) {
                condition = !! cb(e, i, t);
                return !condition;
        }, thisArg);
        return condition;
    }
    Array.prototype._filter = function(cb, thisArg) {
        var arr = [];
        this._forEach(function(e, i, t) {
                if (cb(e, i, t)) arr.push(e);
                return true;
        }, thisArg);
        return arr;
    }
    var arr = new Array(1000);
    for (var i = 0; i < arr.length; i++){arr[i] = i;}

Test runner

Ready to run.

Testing in
TestOps/sec
Homemade _forEach
arr._forEach(function(e){
 e *= 2;
});
ready
Native forEach
arr.forEach(function(e){
 e *= 2;
});
ready
Homemade _map
arr._map(function(e){
 return e * 2;
});
ready
Native map
arr.map(function(e){
 return e * 2;
});
ready
Homemade _reduce
arr._reduce(function(acc, e){
 return e + acc;
},0);
ready
Native reduce
arr.reduce(function(acc, e){
 return e + acc;
},0);
ready
Homemade _reduceRight
arr._reduceRight(function(acc, e){
 return e + acc;
},0);
ready
Native reduceRight
arr.reduceRight(function(acc, e){
 return e + acc;
},0);
ready
Homemade _every
arr._every(function(e){
 return e < 1000000;
});
ready
Native every
arr.every(function(e){
 return e < 1000000;
});
ready
Homemade _some
arr._some(function(e){
 return e > 1000000;
});
ready
Native some
arr.some(function(e){
 return e > 1000000;
});
ready
Homemade _filter
arr._filter(function(e){
 return e%2 === 0;
});
ready
Native filter
arr.filter(function(e){
 return e%2 === 0;
});
ready

Revisions

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

  • Revision 1: published by Daniel Weiner on