Class Pattern Comparisons: Class Loading

JavaScript performance comparison

Test case created by Donald Atkinson

Info

This is a smaller test group for Class Pattern Comparison. For background, it is advisable to go to the full test page

This test evaluates the loading of a class of each pattern. In general, this should not be a determining factor as each class is only defined and loaded once. This is provided for the completeness of the test series.

Class Characteristics

All classes have 3 methods and 2 static methods. In this test, we only use the fully powered Class Module (with Prototypal private access.

Additional Notes

Unlike the other tests, which evaluate different types of access and execution of particular aspects, there is only one version of the Modular Class here.

Preparation code

<!-- Shim from es5-shim: Function.prototype.apply() -->
<script>(function(){var d=!1,b=Function.prototype.apply;try{d=isNaN.apply(null,{})}catch(e){}d||(Function.prototype.apply=function(c,a){return a?"object"!==typeof a||a instanceof Array||!("length"in a)?b.call(this,c,a):b.call(this,c,Array.from(a)):b.call(this,c)})})();</script>
 

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
Prototype Definition
var ProtoObject = function(cfg) {
    var dataPrivate = 0;
    this.dataPublic = 0;
};
ProtoObject.staticMethod = function(){
   return 0;
};
ProtoObject.staticMethod2 = function(){
   return 0;
};
ProtoObject.prototype = {
    doSomething: function(value) {
        return value + 1;
    },
    dummyFn1: function() {
        return 0;
    },
    dummyFn2: function() {
        return 0;
    }
};
pending…
Constructor Definition
var ConstructedObject = function(cfg) {
    var dataPrivate = 0;

    this.dummy = 0;
    this.dataPublic = 0;

    this.doSomething = function(value) {
         return value + 1;
    };
    this.dummyFn1 = function() {
        return 0;
    };
    this.dummyFn2 = function() {
        return 0;
    };
};
ConstructedObject.staticMethod = function(){
    return 0;
};
ConstructedObject.staticMethod2 = function(){
    return 0;
};
pending…
Constructor Definition (Hoisted)
var HoistedConstructedObject = function(cfg) {
    var dataPrivate = 0;

    this.dummy = 0;
    this.dataPublic = 0;

    function doSomething(value) {
         return value + 1;
    }
    this.doSomething = doSomething;

    function dummy1() {
        return 0;
    }
    this.dummyFn1 = dummy1;
    function dummy2() {
        return 0;
    }
    this.dummyFn2 = dummy2;
};
HoistedConstructedObject.staticMethod = function(){
    return 0;
};
HoistedConstructedObject.staticMethod2 = function(){
    return 0;
};
 
pending…
Modular Class Definition
var ClassModuleObject = (function(Class) {
    var syncPrivateAccess = false;

    Class.staticMethod1 = function() {
            return 0;
    };
    Class.staticMethod2 = function() {
            return 0;
    };

    Class.prototype = {
        class: Class,
        doSomething: function(value) {
            return value + 1;
        },
        getPrivateData: function() {
            var result;
            syncPrivateAccess = true;
            result = this.read();
            syncPrivateAccess = false;
            return result;
        }
    };

    Class.create = function(cfg) {
        var dataPrivate = 0;
        function read() {
            if (syncPrivateAccess) return dataPrivate;
        }

        if (this instanceof Class) {
            this.dataPublic = 0;
            this.read = read;
        }
        else {
            return new Class(cfg);
        }
    };

// Return the Actual Class
    return Class;
}(function(cfg){
    return this.class.create.apply(this, arguments);
}));
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