Hello,
I'm currently building a little rpg simulation game in JS mostly. I'm at a point where I'm trying to speed-up my existing code as much as possible.
Why ? Because I'm working on my AI and it does requiere me to be able to compute as many operation in a short time frame as I can. The more the better.
I'm still an order of magnitude slower than what I'd like at this point on my project :/
I've profiled my code over and over (on IE, chrome, FF(firebug) ) and tried to optimized the most called/time consuming part on my code.
I've started with themost common technique I learned (I'm no JS expert^^):
- cache length of my loops (other loop otpimization such a reverse order, remplacing by do/while ect, gave me mixed result)
- changed if/elses to switch when appropriate and to Hash structure for larger tests
- reduce scope traversal (ex: create a local reference to something like MyObject.MyPropertie.ArrayOfItems if its gonna be used more than 2 times)
- reduced all DOM manipulation to a strict minimum (so far, that part seems ok)
I then started to "cache" everything I could to avoid re-computing things for nothing. It gave decent results overall but I'm running out of meaningfull things to cache.
I also took a good look at my code logic overall and optimized all I could think of to reduce the computation made and improve the general flow of my code.
The last thing I came upon is inlining functions. It gave me a huge boost at the expense of code readability / maintainability. I cannot really use that anymore without having a code that will be too dirty to work on later on :/
i'm probably forgetting a few other optimization tricks i've done or tried.
I know that the next big thing would be to look again at my code logic and try to find new design ideas to speed things up but since I'm out of idea there, I figured I'd come here and ask you guys if you have any kind of tips to improve performance for my JS code. I've read a lot of articles on the web but i'm sure I've missed some nice (maybe niche?) tips !
Cheers,