Global Variables vs Local Variables (v57)

Revision 57 of this benchmark created by dodekeract 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 a = 0;
    var c = [];
    
    function x() {
        a = 0;
    }

    function w() {
        var b = 0;
    }

    function arrayGlobal () {
        c = [];
        for (var i = 0; i < 10; i++) {
            c[i] = i;
        }
    }

    function arrayLocal () {
        var d = [];
        for (var i = 0; i < 10; i++) {
            d[i] = i;
        }
    }

    function arrayGlobal2 () {
        c = [];
        c[0] = 0;
    }

    function arrayLocal2 () {
        var d = [];
        d[0] = 0;
    }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Global variables
x();
ready
Local variables
w();
ready
Global Array
arrayGlobal();
ready
Local Array
arrayLocal();
ready
Global Array 2
arrayGlobal2();
ready
Local Array 2
arrayLocal2();
ready

Revisions

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