MindFold
MindFold

Reputation: 771

combining javascript vs image sprite

I've read in some places(i.e headjs website) that loading JS in parallel is better then combining and downloading one big file, then why does css sprites considered better then downloading all the images in parallel?

Upvotes: 2

Views: 202

Answers (4)

Damien Wilson
Damien Wilson

Reputation: 4560

The only downside to bundling your javascript into a single asset (or spiting your images) is the initial time it takes to download; every additional reference to assets a user has already seen are accessed from the browser cache.

The idea behind downloading your javascript bit by bit is to reduce the load time of a user's initial visit. That being said, you generally want to make the smallest number of HTTP requests possible on any given website/application.

Upvotes: 0

Jop de Klein
Jop de Klein

Reputation: 309

Psychotik's and Amjad Masad's are both right - depending on the situation. Typically the less separate resources to download, the better. One thing you want to keep it mind is that if you always aggregate all files you are not going to leverage the full benefits of caching, or end up loading more JavaScript than needed.

For instance, if you have a site consisting of multiple pages (eg. not one RIA / Web application) you would typically not want to aggregate your common scripts together with your per-page custom scripts. I usually create a common.js containing the JS toolkit of choice and scripts used on every page, and an aggregated bundle for each page (or per component in the case of complex RIA / Web Applications).

Compressing related pieces of JavaScript in separate bundles allows for optimal caching while still having a minimal amount of client/server interaction.

Upvotes: 0

psychotik
psychotik

Reputation: 39019

Perhaps what 'they' might mean when they say not to combine JS is that you should consider loading JS only as needed, instead of pulling down all JS that could potentially be needed. So, you first download the JS you absolutely need to get started with the experience on your site and then download the remainder either in the background or as users navigate around your site.

Upvotes: 2

Amjad Masad
Amjad Masad

Reputation: 4035

Downloading one file is better, for the client only initiates one GET request to the server. The same with sprites, I don't know why would they say so.

Upvotes: 0

Related Questions