Global Variables vs Local Variables (v15)

Revision 15 of this benchmark created on


Description

This test compares the performance of global variables to local variables within functions.

Global variables should be slower because they do not exist in a function's activation object. JavaScript must traverse the Scope Chain to locate global variables.

Preparation HTML

<script>
    var obj = {a: new Array()};

    function x() {
        for (var i = 0; i < 10000; i++)
            obj.a.push(i);
        obj.a = new Array();
    }

    function y() {
        var a = obj.a;
        for (var i = 0; i < 10000; i++)
            a.push(i);

        obj.a = new Array();

    }

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Global variables
x();
ready
Local variables
y();
ready

Revisions

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