Back

Some Thoughts on Performance of Object Oriented Javascript

In my previous post on object oriented javascript I was writing how to encapsulate and to hide object functions and data from global accessess. While hanging around I finally solved class inheritance and function overloading, about which I will write in another post, soon. In this little article, I will stress the price of the object orientation in terms of performance.

As I wrote earlier, the traditional approach of writing object orientated code in javascript has little to do with how to code in other object oriented languages. The javascript way is mainly static, knows only global class members, and does not support function overloading well. Dynamic object creation and initialisation is thus not well supported.

The favorised object.prototype approach has a couple of drawbacks, but when it comes to performance it is fast. The prototype of an object contains all the public members of a class, where these members can only access other public members of that class. These class members are simply copied into a newly created instance of a class. I assume that this copying is done on a very low level of the browsers.

Today, I ran a couple of tests and done some optimisations on the code to see, how my dynamic approach to object orientation in javascript performs compared to the traditional approach. The result was pretty clear, public member methods or functions that have access to private member variables, methods, or functions require dynamic class initialisation. This as it showed takes up to ten times more time during object initialisation than the static approach of using the class.prototype.