I’m working on a javascript game for fun, where I have an array of character objects. I’m periodically doing checks on fields within the objects, (like characters[i].xpos, characters[i].ypos, and so on). Is it more efficient to add a function to the prototype of the class to handle moving left and right, and call that periodically, or more efficient to have a global function that adjusts the x and y pos of the character object?
Thanks for your help guys!
Edit:
Thanks for your answers guys. I’ll try to be more specific. I have one global update function that is called with an interval and has a bunch of code in it that changes the properties of an object, like:
globalupdate() {
characters[i].posx++;
characters[i].posy++;
... and more code like that
}
Would it be faster to just write a prototype function within that class and call that? Like:
globalupdate() {
characters[i].update();
}
Thanks for all the quick replies!
I have just read an article on that subject by John Resig (author of jQuery). Judging from that, the most important thing is to not have multiple timers (i.e. intervals and timeouts) because there is a potential for conflict.
So, if your global function will result in having less timers this might very well be beneficial to the overall performance.
Edit: Do I understand your update correctly and is this what you want to know: given a block of code,
and an alternative block of code,
which one would be executing faster?