Private Property Testing (v5)

Revision 5 of this benchmark created by Rich Kosiba on


Setup

var test1 = function() {
        return (function() {
                var private = {};
    
                var init = function() {
                        private.first = 1;
                        private.second = 2;
                        private.third = 3;
                };
    
                var test1 = function() {
                        init();
                };
    
                test1.prototype.first = function(val) {
                        if(typeof val === 'number') {
                                private.first = val;
                        }
                        console.debug(private.first);
                };
    
                test1.prototype.second = function(val) {
                        if(typeof val === 'number') {
                                private.second = val;
                        }
                        console.debug(private.second);
                };
    
                test1.prototype.third = function(val) {
                        if(typeof val === 'number') {
                                private.third = val;
                        }
                        console.debug(private.third);
                };
    
                return new test1();
        }());
    };
    
    var test2 = function() {
        this._first = 1;
        this._second = 2;
        this._third = 3;
    };
    
    test2.prototype.first = function(val) {
        if(typeof val === 'number') {
                this._first = val;
        }
        console.debug(this._first);
    };
    
    test2.prototype.second = function(val) {
        if(typeof val === 'number') {
                this._second = val;
        }
        console.debug(this._second);
    };
    
    test2.prototype.third = function(val) {
        if(typeof val === 'number') {
                this._third = val;
        }
        console.debug(this._third);
    };

Test runner

Ready to run.

Testing in
TestOps/sec
With Private Properties
var test = test1();
test.first();
test.second();
test.third();
test.second(42);
test.second();
 
ready
Without Private Properties
var test = new test2();
test.first();
test.second();
test.third();
test.second(42);
test.second();
ready

Revisions

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

  • Revision 1: published by Rich Kosiba on
  • Revision 2: published by Rich Kosiba on
  • Revision 3: published by Rich Kosiba on
  • Revision 4: published by Rich Kosiba on
  • Revision 5: published by Rich Kosiba on