Class Pattern Comparisons - Full

JavaScript performance comparison

Revision 2 of this test case created by Donald Atkinson on 16th November 2013

Info

This test is composed of smaller test groups and will change according to the addition of these test groups

The ability to have classic object-oriented scoping has been a dividing topic amongst the JS/ES community. In order to accomplish various forms of OO implementation, a variety of libraries often employ complex methods and subclassing structures. While several patterns come close to mimcking the behavior, even these widely used JS class building libraries utilize substantially different tricks and cannot truly mimick classic OO scoping.

The Closed Modular Class Prototype is a single hybrid pattern that provides classic object oriented scoping and most classic inheritance features. This series of tests benchmarks as many aspects as can reasonably be performed. While the entire body of tests will be applied to this test, it may be desirable to take each test independently:

Links to be provided

Test Requirements and Considerations

In order to properly determine the performance statistics, all classes have been made as similarly as possible. This helps to validate the accuracy of the performance in proper comparison to like functionality.

Classes must have the same number of public methods. The reason for this is that Constructor performance drops considerably with the number of methods. Prototype does not. The new pattern ultimately uses a constructor method combined with Prototypal methods.

Classes must have the same number of public data members. This is to ensure the same number of ops per construction.

Accessor functions perform the same operation: calling a single member. So it cannot be argued that one like method is not equal to another like method.

Non-Accessor function simply return a literal value of 0. This is the fastest possible validating return.

Constructors create the same amount of private data. Again, to equal constructor operations.

Data members share the same literal value. Some data types use more ops than another.

Despite the rigidity of the requirements, there are some differences that cannot be accounted for. For instance, the prototype and constructor patterns cannot have private static methods. The prototype method cannot expose private data, though the other two can.

Closed Modular Class Prototype

This pattern is a hybrid pattern between the Constructor, Closure/Module, and Prototypal patterns. Each aspect of the code is used in the exact same way, but in substantially different places in order to achieve classic OO-scoping. Except in the case of object private members, the below is all of the code needed.

In the case of older browsers, the es5-shim for Function.prototype.Array can substantially reduce additional cross-browser boilerplate code.

The Basic Pattern

(function(Class){/* Static Members and Methods Here */Class.create=function(){// This is actually a standard constructor.};

Class Creation

Class creation is simple and the same pattern may be utilized defined normally with a variable, attached to an object, or in an object expression, and even nested another closure/module with no modification.