Cost of using self vs this

JavaScript performance comparison

Test case created by self vs this

Preparation code

<a href="#" id="el">Test</a>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
using this
someNamespace = {
    someVar1: '',
    someVar2: '',
    someEl: document.getElementById('el'),

    someMethod: function() {
        // do stuff
    },

    someOtherMethod: function() {
        // do stuff
    },


    anotherMethod: function () {
        var self = this;

        this.someVar1 = 'whatever';
        this.someVar2 = 'whatever';
        this.someMethod();

        this.someEl.addEventListener("click", function() {
            self.someOtherMethod();
        }, false);
    }
}
pending…
using self
someNamespace = {
    someVar1: '',
    someVar2: '',
    someEl: document.getElementById('el'),

    someMethod: function() {
        // do stuff
    },

    someOtherMethod: function() {
        // do stuff
    },


    anotherMethod: function () {
        var self = this;

        self.someVar1 = 'whatever';
        self.someVar2 = 'whatever';
        self.someMethod();

        self.someEl.addEventListener("click", function() {
            self.someOtherMethod();
        }, false);
    }
}
pending…

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

Compare results of other browsers

0 comments

Add a comment