Monday, August 10, 2015

CH12 - High Performance Web Sites

CHAPTER 12, Rule 12: Remove Duplicate Scripts

  1. Duplicate scripts hurt performance: unnecessary HTTP requests and wasted JavaScript execution.
  2. Make sure scripts are included only once.
  3. One way could be by using a script to check duplication. Rather than using
    <script type="text/javascript" src="asdf.js"/>
    to include a script, programmers can use.
    <?php insertScript("asdf.js")?>
  4. insertScript will check if asdf.js is inserted before or not, it also check if it has other dependencies so it can insert them.

<?php
function insertScript($jsfile) {
       if ( alreadyInserted($jsfile) ) {
            return;
      }
      pushInserted($jsfile);
      if ( hasDependencies($jsfile) ) {
          $dependencies = getDependencies($jsfile);
          Foreach ($dependencies as $script) {
               insertScript($script);
          }
      }
     echo '<script type="text/javascript" src="' . getVersion($jsfile) . '"></script>";
}
?>

No comments:

Post a Comment