for vs reduce

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
        function extend1 () {
                return [].slice.call(arguments).reduce(function (base, obj) {
                        return Object.keys(obj).reduce(function (base, key) {
                                return (base[key] = obj[key], base);
                        }, base);
                }, {});
        };
        function extend2 () {
                var result = {};
                var args = [].slice.call(arguments);
                for (var i = 0, l = args.length; i < l; i++) {
                        var arg = args[i];
                        var keys = Object.keys(arg);
                        for (var j = 0, k = keys; j < k; j++) {
                                var key = keys[j];
                                result[key] = arg[key];
                        }
                }
                return result;
        };
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
reduce
extend1({
        'hoge' : 'base',
        'foo' : 'bar'
}, {
        'hoge' : 'huga'
});
pending…
for
extend2({
        'hoge' : 'base',
        'foo' : 'bar'
}, {
        'hoge' : 'huga'
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment