I’m writing an animation loop and so trying to optimize for speed. This involves sacrificing a lot of readablity, but I’m wondering if at some point it becomes counter-productive. For example, I might start with something like this:
var Animation = Animations[Current_Animation];
var Sequence = Animation.Sequences[Current_Sequence];
var Action = Sequence.Actions[Current_Action];
if (Action.Repeat_Count != Action.Repeat_End) Sequence.Index--;
But when I remove the extraneous variables for optimization, it comes out with truly massive references:
if (Animations[Current_Animation].Sequences[Current_Sequence].Actions
[Animations[Current_Animation].Sequences[Current_Sequence].Index].Repeat_Count
!= Animations[Current_Animation].Sequences[Current_Sequence].Actions
[Animations[Current_Animation].Sequences[Current_Sequence].Index].Repeat_End)
Animations[Current_Animation].Sequences[Current_Sequence].Index--;
My loop is now full of such monstrosities. So I’m wondering if the interpreter having to stop and sort all of that out might actually be slower than using the placeholders. Or does it automagically know what it all means without using any extra CPU?
Thanks!
Your shorter code is more readable and faster on any JS engine I’ve used, not to mention if this is client side JS the download time will be shorter for less having less bytes!
However you should be using the
varkeyword so that all your variables aren’t in the global scope. I’m sure that you have no reason forAnimation,Sequence, andActionto be globals in the above code, since they are really just meant to be temporary variables, but since they are not explicitly declared as local to the current function scope, they are automatically global.