Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Performance: Wrapper-Objects

I have just came across an interesting thing.
Maybe you know that if you create a string
like this: var s = "This is my string.";

that Flash automaticly finds the type and so
Flash will store it as a string.
But if you want to use an string-method, Flash
need to create an wrapper-object for the string that
holds all the string-methods: s.substr(0, s.length);

The creation of the wrapper-object costs time that
you maybe don't have.




I have founded out that if you create your Strings
with the new-operator your String will be an real
Object and the wrapper-object does not need to
be created anymore.
Look at the Test above, their you can see that the
time difference is more than marginal.
This point is guilty for the methods but not for operations.
The string-operations do not need a wrapper-object and
so it is faster to concenate strings with the plus-operator
that don't created with new.
I don't know the player-code so these are only speculations.
SpeedTest = function ()
{
   this.test_arr = new Array();
};
//
SpeedTest.prototype.addTest = function (name_str, test_fnc)
{
   this.test_arr.push({name_str:name_str, test_fnc:test_fnc});
};
//
SpeedTest.prototype.run = function ()
{
   var t;
   for (var i = 0; i < this.test_arr.length; i++)
   {
      t = getTimer();
      this.test_arr[i].test_fnc();
      trace(this.test_arr[i].name_str + ' - needed: ' + (getTimer() - t) + 'ms');
   }
};

// Number of test-loops
c = 10 * 1000;
// Define our Test-Object
sTest = new SpeedTest();
// Add the Tests
sTest.addTest('String(Methods) with new',
function () {
   var s = new String("This is a teststring.");
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = s.substr(0, s.length);
   }
}
);
//
sTest.addTest('String(Methods) without new',
function () {
   var s = "This is a teststring.";
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = s.substr(0, s.length);
   }
}
);
//
sTest.addTest('Number(Operations) with new',
function () {
   var n = new Number(42);
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = n * n + 42;
   }
}
);
//
sTest.addTest('Number(Operations) without new',
function () {
   var n = 42;
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = n * n + 42;
   }
}
);
//
sTest.addTest('String(Operations) with new',
function () {
   var s = new String("This is a teststring.");
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = s + s;
   }
}
);
//
sTest.addTest('String(Operations) without new',
function () {
   var s = "This is a teststring.";
   var dummy;
   for (var i = 0; i < c; i++) {
      dummy = s + s;
   }
}
);
// Run our Tests
sTest.run();
// result
/*
String(Methods) with new - needed: 524ms
String(Methods) without new - needed: 1122ms
Number(Operations) with new - needed: 422ms
Number(Operations) without new - needed: 345ms
String(Operations) with new - needed: 613ms
String(Operations) without new - needed: 415ms
*/

Posted by hOk at June 14, 2003 06:23 PM

New Place