Classes with Inner Function Calls

JavaScript performance comparison

Revision 5 of this test case created by

Preparation code

<script>
  var ClassA = function (state) {
    this.state = state;
};
ClassA.prototype = {

    publicMethod1: function (x) {
        return this.state == 'foo' && this.publicMethod2(x);
    },

    publicMethod2: function (x) {
        return this.state == 'foo' && this.publicMethod3(x);
    },

    publicMethod3: function (x) {
        return this.state == 'foo' && this.publicMethod4(x);
    },

    publicMethod4: function (x) {
        return this.state == x;
    }

};

var ClassB = function (state) {

    function privateMethod1(x) {
        return state == 'foo' && privateMethod2(x);
    };

    function privateMethod2(x) {
        return state == 'foo' && privateMethod3(x);
    };

    function privateMethod3(x) {
        return state == 'foo' && privateMethod4(x);
    };

    function privateMethod4(x) {
        return state == x;
    };

    this.publicMethod1 = privateMethod1;
    this.publicMethod2 = privateMethod2;
    this.publicMethod3 = privateMethod3;
    this.publicMethod4 = privateMethod4;

};

var ClassC = function (state) {
    this.state = state;
};
ClassC.prototype = (function () {

    function privateMethod1(x) {
        return this.state == 'foo' && privateMethod2.call(this, x);
    };

    function privateMethod2(x) {
        return this.state == 'foo' && privateMethod3.call(this, x);
    };

    function privateMethod3(x) {
        return this.state == 'foo' && privateMethod4.call(this, x);
    };

    function privateMethod4(x) {
        return this.state == x;
    };

    return {

        publicMethod1: privateMethod1,
        publicMethod2: privateMethod2,
        publicMethod3: privateMethod3,
        publicMethod4: privateMethod4

    };

})();


var ClassD = function (state) {
    this.state = state;
};
ClassD.prototype = (function () {

    function privateMethod1(self, x) {
        return self.state == 'foo' && privateMethod2(self, x);
    };

    function privateMethod2(self, x) {
        return self.state == 'foo' && privateMethod3(self, x);
    };

    function privateMethod3(self, x) {
        return self.state == 'foo' && privateMethod4(self, x);
    };

    function privateMethod4(self, x) {
        return self.state == x;
    };

    return {

        publicMethod1: function (x) {
            return privateMethod1(this, x);
        },

        publicMethod2: function (x) {
            return privateMethod2(this, x);
        },

        publicMethod3: function (x) {
            return privateMethod3(this, x);
        },

        publicMethod4: function (x) {
            return privateMethod4(this, x);
        }

    };

})();

var ClassE = function (state) {
    this.state = state;
};

ClassE.prototype = (function () {

    var state;

    function privateMethod1(x) {
        return state == 'foo' && privateMethod2(x);
    };

    function privateMethod2(x) {
        return state == 'foo' && privateMethod3(x);
    };

    function privateMethod3(x) {
        return state == 'foo' && privateMethod4(x);
    };

    function privateMethod4(x) {
        return state == x;
    };

    return {

        publicMethod1: function (x) {
            var stack = state;
            state = this.state;
            var result = privateMethod1(x);
            state = stack;
            return result;
        },

        publicMethod2: function (x) {
            var stack = state;
            state = this.state;
            var result = privateMethod2(x);
            state = stack;
            return result;
        },

        publicMethod3: function (x) {
            var stack = state;
            state = this.state;
            var result = privateMethod3(x);
            state = stack;
            return result;
        },

        publicMethod4: function (x) {
            var stack = state;
            state = this.state;
            var result = privateMethod4(x);
            state = stack;
            return result;
        }

    };

})();

var classF = (function () {

    function privateMethod1(x) {
        return this.state == 'foo' && privateMethod2(x);
    }

    function privateMethod2(x) {
        return this.state == 'foo' && privateMethod3(x);
    }

    function privateMethod3(x) {
        return this.state == 'foo' && privateMethod4(x);
    }

    function privateMethod4(x) {
        return this.state == x;
    }

    var ClassF = function (state) {
        this.state = state;
    };

    ClassF.prototype.publicMethod1 = privateMethod1;
    ClassF.prototype.publicMethod2 = privateMethod2;
    ClassF.prototype.publicMethod3 = privateMethod3;
    ClassF.prototype.publicMethod4 = privateMethod4;

    return ClassF;

})();

var instA = new ClassA('foo');
var instB = new ClassB('foo');
var instC = new ClassC('foo');
var instD = new ClassD('foo');
var instE = new ClassE('foo');
var instF = new ClassF('foo');
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Plain prototype
var result = instA.publicMethod1('foo');
pending…
Closure
var result = instB.publicMethod1('foo');
pending…
Prototype + Internal call
var result = instC.publicMethod1('foo');
pending…
Prototype + Internal self argument
var result = instD.publicMethod1('foo');
pending…
Prototype + Internal self state
var result = instE.publicMethod1('foo');
pending…
Closure + prototype
var result = instF.publicMethod1('foo');
pending…

Compare results of other browsers

Revisions

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

0 Comments