captured by Chrome’s dev tools and starting from version 27, Firefox can now log functions that trigger a reflow!
There are a lot of valuable resources on the net about how to track down and fix these issues, so I won’t cover them here. Once fixed, your application should work smoother. Not the case with yours? Keep reading then.
So you made all these checks and you realised that JavaScript is actually what causes your application to perform badly.
Nice, my favourite part of the optimisation process starts here! You need to profile your application.
Grab your modern browser. Start the profiler then your app and interact with it like a normal user would. After a while, stop the profiler.
You can now drill down to the most frequently called functions. If you’re using closures or generated functions, make sure to name them to get meaningful results.
Now, have a look at the logic of the calls. There is certainly room for optimisation. Common mistakes include:
You also need to profile the memory used and track potential leak or heavy garbage collections.
By operating at the code logic level, you can improve the speed of your application. The fixes applied here are not directly related to the JavaScript language, but rather to the logic of your code.
You found and fixed these bottlenecks and now your application is running faster and smoother. You are now done, no need to read further.
You are still reading? I assume you are working on a signal processing tool, a game or a fundamental piece of software that must run fast.
Before looking into the optimisation of JavaScript, I must clarify a couple of things.
Keep in mind that JavaScript VMs are very good at running your code at a fast speed.
Trust the VM.
The second thing is using an alternate JavaScript syntax in your code is likely to never have any significant impact on speed. This belief dates back from the time browsers didn’t have a JIT. Back then, using different syntaxes could make a difference. These kind of statements flourished over the web:
if
are faster than switch
.Optimising JavaScript is not about the syntax. You can’t base an optimisation strategy on syntax rewriting only. You’ll end up wasting your time and get a little to no results. I’m not even mentioning the impact on readability and maintainability of your code.
Also, VM are constantly evolving. What perform best today might not tomorrow. VM implementors take code from the real world to see what developers do and what patterns to optimise. Using weird syntax is likely to never be optimised. Just write your code in a way that makes sense and that should be enough.
After reading and understanding the statements above, you are now ready to improve your code using advanced optimisations. These crazy techniques include:
In the 2nd part of this talk, Vyacheslav shows a tool to inspect the code generated by V8. It’s complex to dive into the VM internals, but that’s the only way to gain some performance points.
To track deoptimisations, using Chrome or V8, you can pass some flags like --trace_deopt
(trace deoptimization). You’ll get a more or less descriptive message about why V8 is not able to optimise your code. Fix it if possible.
There are no such tools for Firefox (since JIT inspector is not working) or IE. So I have to recommend Chrome to optimise JavaScript this way. However, try as much as possible not to be too specific so that your code run fast, or at least not slower, on other browsers too.
Optimisation is a complex topic, but there are techniques we can apply to make your code run fast. I’d like to elaborate on each step described in this process in the form of blog posts. But for now, just take the time to locate the cause of slowness and don’t incriminate JavaScript right away, the culprit can be hiding elsewhere!