this vs self

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function a1(p1, p2) {
        var self = this;
        self.p1 = p1;
        self.p2 = p2;
    }
   
    a1.prototype.test = function() {
        var self = this;
        return self.p1 + self.p2;
    }
   
    function a2(p1, p2) {
        this.p1 = p1;
        this.p2 = p2;
    }
   
    a2.prototype.test = function() {
        return this.p1 + this.p2;
    }
   
};
</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
self
var x = new a1(4,5);
x.test();
pending…
this
var x = new a2(4,5);
x.test();
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