homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript Inline or Cached Page Speed Question
page speed, javascript, inline, cached
webstuck

5+ Year Member



 
Msg#: 4599659 posted 8:59 am on Aug 6, 2013 (gmt 0)

I ran one of my pages through Google's Page Speed service and it recommended that I inline a couple of JavaScript scripts that I use on every page. I have always thought it was best to load them as an external file so that they could be cached and not need to be reloaded for every subsequent page visit. Should I leave them as cached or should I inline them within my web page?

Thanks!
Ben

 

DrDoc

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4599659 posted 4:13 pm on Aug 6, 2013 (gmt 0)

That's an excellent question!

Here are the factors to consider:
  • Script function
  • File size
  • Frequency of use
  • Number of unique visitors
  • Visitor pattern (length of stay, page views, future return visits)

    You must consider all those factors when making a decision like this.

    Script Function
    Is this a script that does something immediately, as soon as the visitor loads the page? Or is it a utility script with functions that are called later?

    File Size
    Is the combined file size only a few bytes, or is it several kilobytes? Due to the way network packet sizes work, the browser may act as if loading a file several times larger than the actual file size.

    Frequency of Use
    On how many pages is the script used? All? Most? Some?

    Number of Unique Visitors
    Do you have a lot of first-time visitors? Again, this comes back to the file size. Would they be better served with a fast-loading inline script than waiting for a second HTTP request to fetch the script file?

    Visitor Pattern
    These last two are really two main factors. In order to benefit from caching, they must visit enough pages to take advantage of the cached script. See the note above regarding file size ... If the effort of downloading an external script is not less than the effort required by the browser to load a few extra bytes embedded within each page, then you have not gained anything by employing caching.

  • Fotiman

    WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



     
    Msg#: 4599659 posted 6:11 pm on Aug 6, 2013 (gmt 0)

    One more thing to consider is Maintainability. I find it's much easier to maintain if the JavaScript is not mixed with the content/markup. Unobtrusive JavaScript.

    DrDoc

    WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



     
    Msg#: 4599659 posted 6:41 pm on Aug 6, 2013 (gmt 0)

    Yes, thank you for bringing that up. Inline JS in static pages is a nightmare. Inline JS in a template is different.

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved