pro optimization tips:

  • Minimize reference use. Use direct values, Use x not obj.x, every time you access a reference it adds O(1). if you access a reference in a loop. then it multiplies the access time, times the looped amount. see all about prefetcher at @23:30 to @55:00
    • Sometimes coping vars and functions to locals helps. local variables are faster to find for the script. and faster to access. fewer references hooping is more efficient as described in above link.
    • Several simple type arrays for each parameter are much faster than an array of objects. described in above link.
    • also helps to make var obj_x=[],obj_y=[],
      instead of var obj=[ {x:1,y:2} , {x:1,y:2} ];
  • To prevent memory duplication on events give anonymous functions a name outside, then attach a named function to the event. if anonymous functions are copied or recreated each time. then they create an unnecessary JIT compilation overhead and memory duplication.
  • Minimize dom access see velocity.js – a fast animation library’s secret sauce. it is faster because less reference hoping when accessing Browser DOM.  So cache some DOM or results.
  • On the fast part of the code, Don’t use jquery $(…) it takes a lot of time , instead use parts from I rewrite all dependency on the library in simple code. , And so I compose simple and specific to my task functions that not call other functions.
  • Functions under 600 chars including spaces and comments are inlined (in Node.js or Chrome V8 Crankshaft), So maybe use Scope to convert long names to short names like (function(){…code here…})() and Use short var names and put comments outside
  • Cache previous results, in a recursive or looping code it is a good idea to cache previously calculated accumulated-to-this-point data and not recalculate it in further loops. this is the idea behind “dynamic programming” text search algorithm. to cache the previously calculated data in a useful way for the further loops.
  • Put everything in memory instead of a database. like an in-memory database.
  • Also maybe with sorted ahead indexes. an index is an array with a value to sort by and a reference. in insertion to array use binary sort next position to figure out next insertion position.


But, First of all, Write Simple and Understandable code. in small functions. So maybe optimize just the important parts.