Loop scope

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = (function() {
        var r = {};
        for (var i=0; i < 1000; i++) {
            r[i] = {
                'number': i,
                'date': new Date().valueOf()
            }
        };
        return r;
    })();
};
</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
outside, same scope
(function() {
   
    var x;
   
    for(x in data){
        var n = x.number
    }
   
})()
pending…
inside
(function() {
   
    for(var x in data){
        var n = x.number
    }
   
})()
pending…
outside, anon function
(function() {
   
    var x;
   
    (function() {
        for(x in data){
            var n = x.number
        }
    })();

   
})()
pending…
outside, declared function
(function() {
   
    var x;
   
    var y = function() {
        for(x in data){
            var n = x.number
        }
    }

    y();
})()
pending…
outside, named function
(function() {
   
    var x;
   
    function y() {
        for(x in data){
            var n = x.number
        }
    }

    y();
})()
pending…
outside, anon function, twice
(function() {
   
    var x;
   
    (function() {
        (function() {
            for(x in data){
                var n = x.number
            }
        })();
    })();

   
})()
pending…
outside, declared function, twice
(function() {
   
    var x;
   
    var y = function() {
        var z = function() {
            for(x in data){
                var n = x.number
            }
        };
        z();
    }

    y();
})()
pending…
outside, named function, twice
(function() {
   
    var x;
   
    function y() {
        function z(){
            for(x in data){
                var n = x.number
            }
        }
        z();
    }

    y();
})()
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