Apr 5 2007

ColdFusion Performance V – The Inheritance Tax

Seems like the British government is not the only body to tax inheritance. ColdFusion will tax you heavily when you extend objects.

I’ve conducted a small test that instantiated 10,000 objects. First, I tried a 3 level inheritance. One object extends another which extends another. The extending objects do nothing. They just sit there with empty bodies, fiddling their thumbs.
I then went for a ‘normal’ 2 level inheritance: one object extends another. And last a single object which extends nothing.
The first case of 3 level inheritance took double the processing time of no inheritance. The 2 level inheritance was dead in the middle. Seems like every time you extend an object you’ll be taxed, even if the extended objects don’t do much!

This confirms my view that in most cases composition of objects is better than inheritance. Instead of extending an object, we should first consider if it might better our cause to pass that object as an argument to a method or make it a property of the parent. In any case, extension, though it has its place, is a less flexible approach than composition.
You can download my test case here. Just unzip the file and place the directory on your ColdFusion server. Note the processing times reported for each test case.

Download: ColdFusion Performance – Object Inheritance