Methods on primitives

Theodore Patrick posted a comparison of calling a method on primitives, once by themself and by creating an object before.

In my opinion the second line is faster than the first one, cause the interpreter can find the method faster from the object list. According to Theodore Patrick its about the garbage collection. The flashplayer creates an object, when you are not working with an instanced primitive and delete it after calling the method.

//-- Testing by Theodore Patrick

//primitive string
a = "Hello World";

//object string
a = new String("Hello World");

i=10000;
while(i--){
	a.split( " " );
}
trace( getTimer() );

Anyway, what you have to keep in mind is, that you have to instance any primitive by new when you need to call a method heavily. Simple computation like a*b or expressions like a = !b are much slower if you are using objects.

This is found is a similar way by holger kohnen in 2003!

More confusion can be found, when comparing primitive values

//a = true;
a = new Boolean( true);
trace( a === true); // false
//------------------------
//a = 3;
a = new Number( 3 );
trace( a === 3 ); // false

Keep this in mind…

1 thought on “Methods on primitives”

  1. thanks for these tips…
    i’ld like to find some optimisations about OOP
    then i could optimise more my FlashPaint package …
    any idea?

Comments are closed.