JPG-CACHE
Don't repeatedly recompute the same information over and over again. Cache, or save, your computed values so you can reuse them. For example,
x = actor.hit / actor.maxHit * (actor.wis - subject.wis) * 100; if ( actor.hit / actor.maxHit * (actor.wis - subject.wis) > 0.5 ) x /= 2;
is much slower than:
y = actor.hit / actor.maxHit * (actor.wis - subject.wis); x = y * 100; if ( y > 0.5 ) x /= 2;
It's also easier to read. The second version is much faster because it stores the computation in a variable rather than recomputing the value from scratch everytime it is needed. Another common thing to cache is object references. For example:
if ( actor.inRoom.sector == 'inside'
|| actor.inRoom.sector == 'underground'
|| actor.inRoom.hasFlag( 'inside' ) )
actor.inRoom.msg( "You're out of the cold now." );
is slower than:
r = actor.inRoom; if ( r.sector == 'inside' || r.sector == 'underground' || r.hasFlag( 'inside' ) ) r.msg( "You're out of the cold now." );
Here the value "actor.inRoom" was used repeatedly so the value was cached.
Last edited by Winterstar in May 2004.