<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Wiredbyte</title>
  <subtitle>A Place to Rant and Muse over all things Tech</subtitle>
  <link rel="alternate" type="text/html" href="http://www.wiredbyte.com/2008/dec/compressing-included-javascript-files"/>
  <link rel="self" type="application/atom+xml" href="http://www.wiredbyte.com/node/150/atom/feed"/>
  <id>http://www.wiredbyte.com/node/150/atom/feed</id>
  <updated>2008-12-11T09:23:34-08:00</updated>
  <entry>
    <title>Compressing included javascript files</title>
    <link rel="alternate" type="text/html" href="http://www.wiredbyte.com/2008/dec/compressing-included-javascript-files" />
    <id>http://www.wiredbyte.com/2008/dec/compressing-included-javascript-files</id>
    <published>2008-12-11T09:22:32-08:00</published>
    <updated>2008-12-11T09:23:34-08:00</updated>
    <author>
      <name>Michael</name>
    </author>
    <summary type="html"><![CDATA[<p>I have been hard at work on Nanobyte, trying to get it ready for Alpha status. I recently worked out file compression to make the pages load faster, and thought I would share.</p>
<p>When I built Nanobyte, I wrote a function to include JS and CSS files with a function that adds each filename to an array for inclusion. With this array, I can combine all of the files into one large file and compress it for viewing.</p>
<p>
<div class="geshifilter">
<ol>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> CompressFiles<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fileArray</span>,<span style="color: #0000ff;">$type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$type</span> != <span style="color: #ff0000;">'css'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$ob</span> = <span style="color: #ff0000;">&quot;&lt;?php </span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ob_start (<span style="color: #000099; font-weight: bold;">\&quot;</span>ob_gzhandler<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$ob</span> = <span style="color: #ff0000;">&quot;&lt;?php</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ob_start(<span style="color: #000099; font-weight: bold;">\&quot;</span>ob_gzhandler<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(<span style="color: #000099; font-weight: bold;">\&quot;</span>Content-type: text/css; charset: UTF-8<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(<span style="color: #000099; font-weight: bold;">\&quot;</span>Cache-Control: must-revalidate<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?&gt;&quot;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$filename</span> = <span style="color: #ff0000;">'./templates/'</span>.THEME_PATH.<span style="color: #ff0000;">'/'</span>.<span style="color: #0000ff;">$type</span>.<span style="color: #ff0000;">'/compressed.php'</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$fh</span> = <span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span>, <span style="color: #ff0000;">'w'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span>, <span style="color: #0000ff;">$ob</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fileArray</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$contents</span> = <span style="color: #000066;">file_get_contents</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span>, <span style="color: #0000ff;">$contents</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$filename</span>;</div>
</li>
<li style="font-family: monospace; font-weight: normal;">
<div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div>
</li>
</ol>
</div>
</p>
<p>This function does several things.<br />
First, we set the output buffer and headers to add to the new file.<br />
We then tell the function where the file is going to be created, and open it for writing.<br />
Once it is open, we write our PHP code block to the file. To get this to work correctly, the file must have a .php extension.</p>
<p>Next, we read the contents of every included file one by one, and append them to our new file. This makes for one large conglomerated file to include.<br />
After all files have been added, we close the file and return the filename of the new file to be included. </p>
<p>Since the page is a php file, the code block will run, which uses ob_gzhandler to compress the file. Using this on Nanobyte, I have reduced a 420KB total page size down to 168KB.</p>
<p>A side effect of this process is that we also reduce the amount of server requests tht our server hs to process. Overall, I gained just over 3 full seconds for the page to load. before this function, the average load time was 6.2 seconds, we are down to 3.19. Incredible!</p>
<p>One last note: Though the file is a .php file, it CAN be included like a regular JS file.. e.g.<br />
<span class="geshifilter"><span class="geshifilter"><code class="geshifilter-drupal6">&amp;lt;script type=&lt;span style=<span style="color: #ff0000;">&quot;color: #ff0000;&quot;</span>&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; src=&lt;span style=<span style="color: #ff0000;">&quot;color: #ff0000;&quot;</span>&gt;&amp;quot;/js/compressed.php&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;</code></span></span></p>
    ]]></summary>
    <content type="html"><![CDATA[<p>I have been hard at work on Nanobyte, trying to get it ready for Alpha status. I recently worked out file compression to make the pages load faster, and thought I would share.</p>
<p>When I built Nanobyte, I wrote a function to include JS and CSS files with a function that adds each filename to an array for inclusion. With this array, I can combine all of the files into one large file and compress it for viewing.</p>
<p><div class="geshifilter"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> CompressFiles<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fileArray</span>,<span style="color: #0000ff;">$type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$type</span> != <span style="color: #ff0000;">'css'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$ob</span> = <span style="color: #ff0000;">&quot;&lt;?php </span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ob_start (<span style="color: #000099; font-weight: bold;">\&quot;</span>ob_gzhandler<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$ob</span> = <span style="color: #ff0000;">&quot;&lt;?php</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ob_start(<span style="color: #000099; font-weight: bold;">\&quot;</span>ob_gzhandler<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(<span style="color: #000099; font-weight: bold;">\&quot;</span>Content-type: text/css; charset: UTF-8<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(<span style="color: #000099; font-weight: bold;">\&quot;</span>Cache-Control: must-revalidate<span style="color: #000099; font-weight: bold;">\&quot;</span>);</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?&gt;&quot;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$filename</span> = <span style="color: #ff0000;">'./templates/'</span>.THEME_PATH.<span style="color: #ff0000;">'/'</span>.<span style="color: #0000ff;">$type</span>.<span style="color: #ff0000;">'/compressed.php'</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$fh</span> = <span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span>, <span style="color: #ff0000;">'w'</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span>, <span style="color: #0000ff;">$ob</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fileArray</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$contents</span> = <span style="color: #000066;">file_get_contents</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span>, <span style="color: #0000ff;">$contents</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fh</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$filename</span>;</div></li>
<li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li></ol></div></p>
<p>This function does several things.<br />
First, we set the output buffer and headers to add to the new file.<br />
We then tell the function where the file is going to be created, and open it for writing.<br />
Once it is open, we write our PHP code block to the file. To get this to work correctly, the file must have a .php extension.</p>
<p>Next, we read the contents of every included file one by one, and append them to our new file. This makes for one large conglomerated file to include.<br />
After all files have been added, we close the file and return the filename of the new file to be included. </p>
<p>Since the page is a php file, the code block will run, which uses ob_gzhandler to compress the file. Using this on Nanobyte, I have reduced a 420KB total page size down to 168KB.</p>
<p>A side effect of this process is that we also reduce the amount of server requests tht our server hs to process. Overall, I gained just over 3 full seconds for the page to load. before this function, the average load time was 6.2 seconds, we are down to 3.19. Incredible!</p>
<p>One last note: Though the file is a .php file, it CAN be included like a regular JS file.. e.g.<br />
<span class="geshifilter"><code class="geshifilter-drupal6">&lt;script type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> src=<span style="color: #ff0000;">&quot;/js/compressed.php&quot;</span>&gt;&lt;/script&gt;</code></span></p>
    ]]></content>
  </entry>
</feed>
