SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question More than 1 JS file? When to split?

    When should you break down your JS into more than 1 file? I would think that you should do it when 1 file gets to meet a certain size, but what would be the advantage of breaking it down in the first place since no matter what, it will be the same amount of memory having to be read by the browser...?

    Any info on this would be great. I'm sure I'm missing something here as I have one hell of a thick head when it comes to JS...

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The advantage of multiple script files is that you can load a small script before the page is rendered, and use that code to add more code as needed.

    It has the biggest benefit on large or frequently updated sites, with many pages selecting from a basic menu of scripts.

    Its disadvantages include needing more http requests to load your pages and more code- a lot of it for checking for dependencies before using objects defined in other files.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,824
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The biggest advantage in splitting up the JavaScript is to only load those parts that a web page actually uses rather than loading all of it in each page. Progressively loading pieces of code as required is not always necessary but would be a logical next step if your JavaScripts are still too big (say over 7k total combined size).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •