I was searching for ways and tricks to improve my javascript code performance.
I would like to know what are the best practice to use in order to have best performance.
Does the position of the script (in the header, in the body, in the middle of the code or at the end) affect the performance?
Thanks for any tips.
If there is a lot of JS, put it at the end of the document. Although this makes no difference on load time, the user will see the page sooner and can begin to read it while your js loads, as opposed to seeing nothing until your JS is downloaded (which is what happens when you put it in the head). It merely makes the download appear faster. This would also solve the problem mentioned above about the script executing on an unfinished document, although for that, an even better solution is to use window.onload().