Global Variables vs Local Variables (v14)

Revision 14 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:0, b:0};

    function x() {
        for (obj.a = 0; obj.a < 20; obj.a++)
            obj.b += obj.a;
    }

    function y() {
        var loc = {a:0, b:0};
        for (loc.a = 0; loc.a < 20; loc.a++)
            loc.b += loc.a;
    }


    function w() {
        var loc = obj;
        for (loc.a = 0; loc.a < 20; loc.a++)
            loc.b += loc.a;
    }
</script>

Test runner

Ready to run.

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

Revisions

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