{"id":1612,"date":"2013-06-30T21:43:46","date_gmt":"2013-06-30T21:43:46","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=1612"},"modified":"2013-08-23T22:38:31","modified_gmt":"2013-08-23T22:38:31","slug":"reducing-javascript-load","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/reducing-javascript-load\/","title":{"rendered":"Reducing your JavaScript load"},"content":{"rendered":"<p><a href=\"http:\/\/www.sitekickr.com\/blog\/wp-content\/uploads\/2013\/06\/load-time-js-http-graphs.png\"><img loading=\"lazy\" align=\"right\" alt=\"load-time-js-http-graphs\" border=\"2\" class=\"alignright size-full wp-image-1616\" height=\"465\" src=\"http:\/\/www.sitekickr.com\/blog\/wp-content\/uploads\/2013\/06\/load-time-js-http-graphs.png\" style=\"padding:5px;margin-left: 5px;\" width=\"256\" srcset=\"https:\/\/www.sitekickr.com\/blog\/wp-content\/uploads\/2013\/06\/load-time-js-http-graphs.png 256w, https:\/\/www.sitekickr.com\/blog\/wp-content\/uploads\/2013\/06\/load-time-js-http-graphs-165x300.png 165w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/a>I&#39;ve written a couple articles on this blog that speak to the <a href=\"http:\/\/www.sitekickr.com\/blog\/javascript-optimization-merging-files\/\">advantages of keeping all of your JavaScript in a single file<\/a>, even it&#39;s not used on every page (running portions of the script conditionally based on url, existence of an element or some other factor).<\/p>\n<p>The graph at the right explains this concept visually. Because each JavaScript file requires a separate HTTP connection to load it, the load time increases exponentially as the number of individual files load increases. So, have loading 10 files, each with 50 lines of code will take a great deal longer to load than a single file with 500 lines of code.<\/p>\n<p>There are cases, though, where it doesn&#39;t always make sense to lump a piece of JavaScript in the main script file. Two reasons come to mind:<\/p>\n<ol>\n<li>You don&#39;t want a certain piece of JavaScript to have the same caching directives as the main file.<\/li>\n<li>A piece of JavaScript is relatively large, but is very seldom executed.<\/li>\n<\/ol>\n<p>In both instances above, it does actually make sense to create a separate script file for this piece of code. When it&#39;s loaded, you&#39;ll of course have the extra HTTP connection. But, you can still load the file asynchronously from the main JavaScript file as needed.<\/p>\n<p>By including the &quot;secondary&quot; JavaScript file from the &quot;primary&quot; JavaScript file, you allow the primary JS file to become a controller, which helps keep your script organized. It also allows you to load your script in a far more consistent fashion than if you had added a &lt;script&gt; tag to the individual page that requires it.<\/p>\n<p>The jQuery code below demonstrates this:<\/p>\n<pre class=\"prettyprint linenums\"><code>\r\nif ($(&#39;body.edit-page.edit-location&#39;).length !== 0) {\r\n  (function() {\r\n    var js = document.createElement(&#39;script&#39;);\r\n    js.type = &#39;text\/javascript&#39;;\r\n    js.async = true;\r\n    js.src = &#39;\/src\/js\/location-edit.js&#39;;\r\n    var scriptTag = document.getElementsByTagName(&#39;script&#39;)[0];\r\n    scriptTag.parentNode.insertBefore(js, scriptTag);\r\n  })();\r\n}\r\n<\/code>\r\n<\/pre>\n<p>As a final note, you&#39;ll notice that we&#39;ve wrapped the asynchronous JavaScript file load in a function block. We do this so we don&#39;t have to create the <em><strong>js<\/strong><\/em> variable in the global namespace. This will prevent variable naming conflicts, as well as keep your code a little cleaner (the js variable is destroyed after the script is loaded).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although it typically makes sense to pack all of your JavaScript into one file to improve page load time, there are cases where it makes more sense to house certain chunks of JS code in a separate file. Here&#8217;s a trick to help keep things organized.<\/p>\n","protected":false},"author":1,"featured_media":1619,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[238,5],"tags":[29],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1612"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=1612"}],"version-history":[{"count":9,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1612\/revisions"}],"predecessor-version":[{"id":1675,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1612\/revisions\/1675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/1619"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}