A hierarchy of optimization levels exists for JavaScript, what Bentley and others call design levels. [6] First comes the global changes like using the right algorithms and data structures that can speed up your code by orders of magnitude. Next comes refactoring that restructures code in a disciplined way into a simpler, more efficient form [7] ). Then comes minimizing DOM interaction and I/O or HTTP requests . Finally, if performance is still a problem, use local optimizations like caching frequently used values to save on recalculation costs. Here is a summary of the optimization process:
Choose the right algorithm and data structure.
Refactor to simplify code.
Minimize DOM and I/O interaction.
Use local optimizations last.
When optimizing your code, start at the highest level and work your way down until the code executes fast enough. For maximum speed, work at multiple levels.