# Not all javascript files for asset pipeline:

**URL:** <https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776>\
**Category:** Rails\
**Created:** [October 17, 2012, 1:47pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776 "2012-10-17T13:47:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 17, 2012, 1:47pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/1 "2012-10-17T13:47:19Z")

</div>

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

in the page that requires the particular file.

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

soichi

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 17, 2012, 1:56pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/2 "2012-10-17T13:56:05Z")

</div>

On Wed, Oct 17, 2012 at 6:47 AM, Soichi I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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 %\>

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 17, 2012, 2:00pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/3 "2012-10-17T14:00:46Z")

</div>

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’

On Wed, Oct 17, 2012 at 5:17 PM, Soichi I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## directory, and

–  
Thanks,  
Aash

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 17, 2012, 2:42pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/4 "2012-10-17T14:42:15Z")

</div>

On 17 October 2012 12:47, Soichi I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 17, 2012, 4:01pm UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/5 "2012-10-17T16:01:01Z")

</div>

Soichi I. 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:

> **[The Asset Pipeline — Ruby on Rails Guides](https://guides.rubyonrails.org/asset_pipeline.html#in-production)**
>
> This guide explains how to handle essential asset management tasks.After reading this guide, you will know: What is an asset pipeline. The main features of Propshaft, and how to set it up. How to migrate from Sprockets to Propshaft. How to use other...

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.

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 18, 2012, 9:07am UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/6 "2012-10-18T09:07:48Z")

</div>

On Wed, Oct 17, 2012 at 7:22 PM, Soichi I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 18, 2012, 2:22am UTC](https://www.ruby-forum.com/t/not-all-javascript-files-for-asset-pipeline/223776/7 "2012-10-18T02:22:05Z")

</div>

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.
