Developer Tips: Bypass JavaScript’s Garbage Collections

According to Google performance expert Colt McAnlis, one of the direst problems web developers face today is JavaScript performance. JavaScript parsing engines use GC (garbage collection) for memory management, a method that allows programmers make sure memory no longer used by a program is sent back to the operating system, and though that may be helpful, memory management code in languages like C and C++ is a time consuming process that doesn’t offer native support by browsers. In addition, JavaScript engines may cause applications to slow down due to its tendency to launch GC routines at random, thus spiking performance times in applications, or for instance, drop frame rates in video apps. If a memory-hungry application is added to the equation, JavaScript can devastate system performance, especially in devices that cannot provide multitudes of system memory in relation to the amount of memory being used by the app. Extended use of closures in the code, a method widely used to expand availability of locally defined variables, is another factor piling onto system performance issues. McAnlis uses JQuery as an example, a popular JavaScript library, that’s known to bear various memory relocation issues, due to its wide use of closures. Libraries used for high performance HTML5 should be an example for developers, according to McAnlis, who cites Emscripten as an ideal solution. Emscripten turns C or C++ code into JavaScript and helps developers pre-allocate memory blocks. This method offers a way to bypass Garbage Collections, offering the programmer the option to decide when memory is no longer needed and can be returned to the system. McAnlis states that programs compiled with this method can offer improved performance of up to four times higher speed, as well as help avoid system lagging, often caused by JavaScript’s Garbage Collections.

Add new comment