Not all javascript files for asset pipeline:

Rails 3.1.3

Not all javascript files are necessary for every page of app. If I put all JavaScript files in app/assets/javascripts/ , all of them are compiled and read in every page. I believe this is a waste of memory space and CPU load.

Is there anyway to avoid it?

I suppose we can put some javascript files in lib/assets/javascripts/ directory, and

<script type="text/javascript" src="/lib/assets/javascripts/geodesic.js"></script>

in the page that requires the particular file.

Does this work? Because I cannot figure out the route required for this.

soichi

Not all javascript files are necessary for every page of app. If I put all JavaScript files in app/assets/javascripts/ , all of them are compiled and read in every page. I believe this is a waste of memory space and CPU load.

Why are you serving assets from Rails /period/ without caching if you are so worried about CPU load? I would be more worried about the user load time than my own CPU usage on a server for such a trivial action (trivial because you can cache it to a static file)

Is there anyway to avoid it?

Stop requiring the appname.js file since it uses sprockets to require the entire tree, include each individual file. Or you could adjust the appname.js file and stop it from requiring each asset in the tree but I prefer just to delete it entirely and include the specific files I need.

Example: <%= javascript_include_tag :jquery %> On some:     <% if @page && @page.role == :post %>       <%= javascript_include_tag :jquery %>     <% end %>

I think the file that you do not want to compile using asset pipeline should be removed from the manifest file in app/assets/javascripts/ - “application.js”.

If you want some of the files that belong to a specific page, then it’s better to create a manifest file: “file_name.js”. Include the files that you need to compile and then in your specific view, write:

javascript_include_tag ‘file_name’

Rails 3.1.3

Not all javascript files are necessary for every page of app. If I put all JavaScript files in app/assets/javascripts/ , all of them are compiled and read in every page. I believe this is a waste of memory space and CPU load.

Are you worried about the server or the user's PC running the browser? If it is the user then you need not worry. I believe that the browser will only fetch the file once for each session, it will not fetch it for every page. Similarly on the server they will only be compiled once each time you change anything, and will only be served to the browser when it asks for them.

Colin

Soichi Ishida wrote in post #1080153:

Rails 3.1.3

Not all javascript files are necessary for every page of app. If I put all JavaScript files in app/assets/javascripts/ , all of them are compiled and read in every page. I believe this is a waste of memory space and CPU load.

Yeah this is a non-issue. Assets are pre-compiled and cached in production server side as described here:

And client browser will also cache them so they won't get loaded on every page.

But even worse case scenario and somehow things keep getting reloaded, unless you've got some massive framework with 200+.js files, the processing overhead from serving a few javascript files is really miniscule. I wouldn't bother with including scripts only in the pages that use them. Keep things simple for yourself and include everything in your application layout.

Thanks everyone. I was worried about both server and client but especially about the client side.

Sounds like I should not be worried about it too much.

Now days Javascript parser themselves are pretty efficient, the only problem I worry about with Javascript is how much I'm loading at once, and that's not to do with processing but more load time, if I can load 1kb vs 10kb I will go that route because even though most of us don't like to admit it, some people still do have bad 3G and even dial-up or satellite that isn't so stable on speed. So I always reduce how much I'm loading at once if I can, meaning I treat my front-end like I treat my back-end I only load and use what I absolutely need not what I think will be cool and if I can pull in the cool pieces without a larger dependency I will. What I mean by that is I actually take specific pieces from Gems and just put them in the lib, or I pull specific pieces from Javascript libraries and load them pieces myself.

Another side rant on the same subject that some people should actually worry about: The other thing I worry about is what some companies like Twitter do which is activate the GPU for no reason other than rendering CSS animations (which isn't even necessary on a modern computer unless you do quite a few things) or other stuff which isn't always needed at all and in some cases makes things lock up (for a split second) on some smart phones.

It makes it even worse when you mix bootstrap with Chrome on Linux which has a huge bug in GPU rendering (with FGLRX and in some cases even Nvidia) in that any page that is rendered by the GPU is completely broken. So if you are worried about the client, disable the GPU enabled parts of your CSS or warn your users to disable the GPU usage in Chrome.