Javascript Inheritance benchmarks, redux

The other day I posted examples of Douglas Crockford’s parasitic Javascript inheritance method. I’d like to qualify some of my claims with some benchmarks using JSLitmus. The skinny: Parasitic inheritance is, indeed, faster. It’s slower to initialize, but unless you’re creating thousands and thousands of instances, that shouldn’t be a problem. It’s faster on calling superclass methods, and calling non-overridden methods. And you don’t need a bulky library to do it. Read on for the results.I did these tests on an intel dual core 1.86ghz. I tried both the current FireFox (3.07), and Chrome. Here are the results; explanation will follow. If you’re so inclined, you can take a look at the JSLitmus testbed I’m using and try them yourself.

Chrome 1.0.154.48 Benchmark

Chrome 1.0.154.48 Benchmark

Unfortunately, with Chrome several of the methods return in infinite time. That’s pretty efficient. You can see that they’re mostly the test runs that run non-overridden functions - calling a method from the subclass that is only defined in the superclass. To really test which of these is more efficient, we’ll have to bring out an older browser. But first, let me explain the captioning on this graph.

The tests labeled Parasitic are using the JavaScript Inheritance method outlined in my previous post. The other labels are described originally at Broofa.com’s article on Javascript inheritance performance:

  • Adhoc is a style that can’t really be considered true inheritance. It’s fast, but unwieldy.
  • Resig is  a Base2 variant that the creator of jQuery was experimenting with.
  • Base is Base2, from Dean Edwards’ library.
  • Proto is the method used in the Prototype library.

The tests labeled, “Call method in type superclass,” refer to calling a superclass method from within the subclass method. Those labeled “Call non-override in type subclass” are calling a method that is defined in the superclass, but not overridden in the subclass. For my parasitic style tests, I’m first calling the superclass method without any context - this in the superclass method would refer to the global scope. With context, I’m accessing the superclass method with the .call method common to all function objects and passing it this, so it runs in context of the subclass.

Here we can see some real metrics (though, it also outlines how much faster Chrome is compared to the current public stable release of Firefox). Calling non-overridden functions is, just like before, negligable in difference between the different styles of inheritance. However, when you’re calling superclass methods from within an overriding subclass method, Parasitic inheritance is much faster. It’s faster because it follows the first rule of optimization: do less: it only uses built in Javascript method calls to accomplish its task.

My suggestion? If you can get away with it, don’t use a library for building your Javascript inheritance chains. You don’t need it, and it only slows things down. Especially if you’re using the Prototype library. It requires some more indepth knowledge of how Javascript works, but the syntax is more understandable - and it performs better. The only time it lags behind is when instantiating the objects, but I’d rather optimize based on execution instead of initiation - especially if you’re working on a large scale application.

Reblog this post [with Zemanta]

Your email:

 

Tags: , , , , , , ,

Leave a Reply