Location of javascript script block
Alec
Posted on
Nov 21 2014
I was looking to discuss where the Javascript script block should go on the page. It seems like it should be early to avoid failing to catch errors but general recommendations on performance are to put javascript near the bottom of the page. Thoughts?
Callum
Posted on
Dec 10 2014
Apologies for the delay in response - this would naturally depend on the nature of your site and where the bulk of code is being performed (i.e static site with much javascript processing performed up front vs SPA with much code called asynchronously later on via callbacks etc). This choice would dictate whether you are expecting errors in the header scripts and need to catch them, or are fine waiting until the bottom of the page as you expect errors in response to events etc during the page lifecycle.
The minified filesize at 18kb is relatively small to the average website and other common libraries so there shouldn't be much of an effect placing the script reference in the header (especially considering if you are bundling it along with the page or loading it from the CDN as documented here). If this delay would be unacceptable and lifecycle errors are expected then placing it at the bottom of the body but above the rest of your scripts would be an acceptable approach.
The setup procedure, assuming you're calling init() then attach(), runs a negligible amount of code and won't impact perf even on mobile devices.