PDA

View Full Version : Cache Array-Object References?


Terry
06-17-2003, 04:24 AM
Hi,

As far as speed goes which version of the following code would be faster... this is part of a larger program I'm working on and I was wondering if it makes any difference to cache a reference to an array object:


function initDiceImages()
{
var iDice = new Array(),i = 1;
if(document.images)
{
for(i; i < 7; i++)
{
iDice[i] = new Image(56,56);
iDice[i].src = "images/d" + i + ".gif";
}
iDice[0] = new Image(56,56);
iDice[0].src = "images/empty.gif";
iDice['heldOff'] = new Image(56,14);
iDice['heldOff'].src = "images/heldOff.gif";
iDice['heldOn'] = new Image(56,14);
iDice['heldOn'].src = "images/heldOn.gif";
iDice['rOff'] = new Image(340,29);
iDice['rOff'].src = "images/RollDice.gif";
iDice['rClick'] = new Image(340,29);
iDice['rClick'].src = "images/RollDiceClick.gif";
}
return iDice;
}


or...


function initDiceImages()
{
var iDice = new Array(), d = iDice,i=1;
if(document.images)
{
for(i; i < 7; i++)
{
d[i] = new Image(56,56);
d[i].src = "images/d" + i + ".gif";
}
d[0] = new Image(56,56);
d[0].src = "images/empty.gif";
d['heldOff'] = new Image(56,14);
d['heldOff'].src = "images/heldOff.gif";
d['heldOn'] = new Image(56,14);
d['heldOn'].src = "images/heldOn.gif";
d['rOff'] = new Image(340,29);
d['rOff'].src = "images/RollDice.gif";
d['rClick'] = new Image(340,29);
d['rClick'].src = "images/RollDiceClick.gif";
}
return d;
}


Doesn't the former have to evaluate the array reference every time I assign a value to an index?

Terry

beetle
06-17-2003, 05:44 AM
No - you're 2nd is actually slower - since iDice is the actual array and exists in the current context - accessing it via the reference takes longer, but most likely negligbly so. If you had to access several other objects before getting to iDice (not in the current context) then your reference would help.

Terry
06-17-2003, 06:13 AM
Thanks,

It makes sense. That array just get's the images cahed and then I assign the array to an object var, like so:

this.dice_imgs = initDiceImages();

I have another question. What if I cached a reference to the main object in my methods, like:


Game.prototype.getHeld = function()
{
var t = this, holds = t.record['Hold'];
if(t.record['Roll'] >= 3)
return;
var len = holds.length, i = 0;
for(i; i < len; i++)
{
if(holds[i]) // if held, skip
continue;
var r = t.getRand();
document.images['dice' + i].src = t.dice_imgs[r].src;
t.record['DiceVal'][i] = r;
}
t.record['Roll']++;
t.chgImg("RollNum",t.num_imgs[t.record['Roll']].src);
//alert(t.record['DiceVal'].join(' - '));
}


Is this any different than the other example. I mean if I put the this keyword on every assignment wouldn't "this" have to evaluate every time?


Terry

beetle
06-17-2003, 06:17 AM
Hmmm - I suppose - but I think that's a pretty immediate operation. I still think storing a reference in this case is overkill and unecessary.

I don't know for sure - but using the reference does add to overall brevity ( assuming you use a single-char reference variable)

Personally - I'd stick with using this

Terry
06-17-2003, 06:27 AM
Your might be right, it probably is unecessary ... just thought I'd get an opinion form some experts. I could of sworn though that on the larger methods in my class, caching "this" seemed to speed up things??

I just got done reading this:
http://www.informit.com/isapi/product_id~%7B261A0D6B-3123-4A39-9428-623E8B5691DD%7D/content/index.asp

... trying to implement some of the suggestions. Great article!

Terry

beetle
06-17-2003, 06:32 AM
I made a StopWatch (http://www.peterbailey.net/dhtml/showsrc.php?file=stopwatch) class for timing stuff - if you'd like to know how stuff works out.

Terry
06-17-2003, 06:39 AM
Looks good. I'll try it out. You have a lot of good stuff on your site. Great work :)

Terry

beetle
06-17-2003, 06:46 AM
Thanks - and that is a good article (which I've read before)

I've been trying to write an article about JS scope for weeks (months) but I just can't ever seem to finish it!