Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm starting on Javascript and learning best practices, but I haven't found anywhere saying which is better:
1) Separate scripts:
<script>
// script A here
</script>
<script>
// script B here
</script>
2) Putting scripts together
<script>
// script A here
// script B here
</script>
Neither. Since scripts are usually shared between pages, it's better to keep them separate from your HTML:
<script type="text/javascript" src="urlOfScriptA.js"></script>
<script type="text/javascript" src="urlOfScriptB.js"></script>
If you cancombine them, you save a request, so it's even better:
<script type="text/javascript" src="urlOfCombinedScript.js"></script>
But for development it can be handy to have separate files. You can use tools to combine and minify your scripts. That way, not only are they combined into one script (resulting in less requests), but also they are compressed as much as possible, saving bandwidth and loading time.
For now, I wouldn't worry about that yet. Keep the files separate if you think that is easier to work with. And then, later, you can find a tool that can combine and minify them for you. Since this is a slow process, you shouldn't do that on the fly, and it's inconvenient to do it in a development environment, so only combine and minify them when you publish the site.
Organisation.
Do the scripts do stuff directly related to each other?
If yes: Merge.
If no: Keep separate.
(Although ideally they should be in external .js files according to the same rules of separation, then merged by a compiler/minifier before being served)
It's best practice to put your Javascript on the server as a single file (to prevent files loading in the wrong order).
You can still work in individual files, but use some concatination / minimization software to compile it.
There are lots of different ways to achieve this, but it generally depends on your working environment.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Right now, I work on multiple css and js files but I combine them all together in 1 big file. I am not sure if this is the right way. For example my js file is about 200kb(Foundation + jQuery). This file is only loaded at the end of the body, so I am not sure what is the best way, I mean the file size won't get smaller anyway if I split them up.
You should always combine your CSS and JS files into a single file per type as a general rule.
However, if you really want to play around with load optimization, you can try to take advantage of the amount of simultaneous downloads a browser can do at one time. If it can do 6 at a time, then you won't see any major performance hit, and you may even get a performance gain, for having up to 6 separate files so they can download in parallel.
I personally wouldn't count on the simultaneous downloads though. It is a better rule of thumb to just combine them.
You can always use Googles PageSpeed to review your site for load optimization.
You should always combine and minify CSS and JS files. The browser makes fewer a separate request for each file you have. That's a small hit, but if you have a lot of files, it adds up.
https://developers.google.com/speed/docs/insights/MinifyResources
https://blog.hubspot.com/marketing/reduce-http-requests
Task runners like Grunt and Gulp can help with this. You can set up a script to minify and combine (concat) your files all in one action.
Or your IDE might even have it built in, so that everytime you save, a new file is compiled.
Always make sure your JS is right before the closing body tag, unless you have a specific reason to put it higher on the page.
The best solution for optimizing a website is firstly
1. To reduce the number of files to be loaded which reduces the number of browser requests,
2. To use server cache, to removing the repetition of MySql queries or connections
3. To use browser cache for the bigs javascript files
4. Synchronous and asynchronous requests or Ajax/Jquery
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a web tool using C#, HTML and JavaScript code based on the MVC pattern and I was wondering if it is smart (page load time, traffic, etc.) to load all needed scripts in the _Layout.cshtml.
Is there a difference in bundling all scripts together and load them in the head section of the _Layout.cshtml or load them in the actual views when needed?
Is there a best practice for this matter?
The best strategy is to "load what you really need". You should bundle all shared scripts and styles on one side and load that on the layout, and then create separate bundles for each view to load only what will be used on that particular page.
The difference would be that you will require more bundling configuration (as you would have one bundle per view), but IMHO, you will gain a lot more in a better code organization and avoid downloading unnecessary files.
Of course this all depends of what kind of application you are developing. But if you have all in one bundle and the project gets larger, it will be very difficult to later change the strategy as you will need to review all the dependencies on each view.
In terms of page load/traffic, it is clear that this strategy is better as the client will download only the files which are needed.
you can load script bundles in a view like this
#section scripts {
#Scripts.Render("~/bundles/movie")
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have some questions about Javascript.
Most of my website's pages use js. For better performance, what should i do ? :
Write all of my js code in one file and link it with my pages ? Or one js file per page ?
Is it a bad manner that i never mix js into my html ? I like to have separate things (html, css, js)
Thank you.
For maintainability
Keep separate pages while developing the app, so that any one can make out which feature has been coded where.
For performance
Before deploying it for production, minify your js and css files so that there are less network calls to download those files.
Is it a bad manner that i never mix js into my html ?
No, having non-intrusive js is both good for readability and it is good for performance too since it gives you a chance to minify the js files. You can't minify inline js.
My experience is writing a relatively big website in ASP.MVC .
I did not write all the javascript code in one file because that would have been difficult to manage. I made a folder that included multiple javascript files, each having its own purpose.
If the javascript is more than say 10 lines of code, and you can put it outside the html file, i would advise so, because it is faster to look at .
It is easier to just have one .js file for all pages, but it is better for readability if you have multiple files.
Mixing javascript into HTML can be useful but it depends on your needs.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for simple bullet point answers please. I've tried looking all over, Googling, other questions here but I can never find both advantages and disadvantages for each method.
This is the answer I got from W3Schools pertaining to external javascript files
Pros
It allows separation of concerns - which is not a big deal in simple pages but as the script grows larger you can have a monolithic html page. Big files in general are not ideal for maintainability
It allows caching - when the browser loads a script externally (whether it's be from your site or a cdn) it caches the file for future use. That's why cdn's are preferred for commonly used scripts. Makes the browser use a cached script instead of building a new one every time the page loads which makes the page load faster
More readable code - this ties into the first bullet point but nevertheless it is important. The smaller the files we humans are working with the better. It is easier to catch mistakes and much easier to pass of the torch to the next developer working on the project or learning from it.
Cons
The browser has to make an http request to get the code
There may be other browser specific reasons as well, but I believe the main reason is the separation of code into different components.
Probably the best advantage of using external javascript files is browser caching - which gives you a good performance boost.
Imagine you have a site that uses MyJsFile.js (a random 50kb javascript file that adds functionality to your websire).
You can:
embed it in every page, and add the 50kb to every page request (not ideal)
link it in every page (<script src="MyJsFile.js"></script>)
The second option is usually prefered because most modern browsers will only get the file once, and serve it from the browser cache instead of downloading it at every request.
Check out similar questions:
Why not embed styles/scripts in HTML instead of linking?
When should I use Inline vs. External Javascript?
Is it better to put the JS code on the html file or in an external file?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Here's an example from Bootstrap's "Getting Started" code:
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js"></script>
Putting CSS and JS onto a CDN seems like pure lunacy. If the CDN goes down, so does your site. I gather that pointing to a CDN gives you the programmer the latest updates and all, but what if the maintainers break their own production code? I always download the necessary css and js and park them in my own directory. Whats the consensus?
TL;DR Why leave critical files such as JS and CSS to a CDN?
Reliability is stellar for all three CDNs (Google vs. Microsoft vs. Media Temple CDNs)
First off, all three CDNs proved to have excellent availability. The only CDN with any downtime at all for this entire year so far was Microsoft’s, and that was just a few minutes. All three had, when rounded to two decimals, 100.00% uptime.
This really is what you want from a CDN. You shouldn’t have to worry about it working or not, and the distributed nature of a CDN usually makes it extremely reliable, as this survey shows.
We will focus the rest of this article on performance since that is where the true differences are.
SOURCE
So the question is. Your host can do better?
The use of the CDN is to allow for edge caching and improved performance in page loading. Usually you define a fall back reference in the case the CDN is down. Here is another answer that describes this process: How to fallback to local stylesheet (not script) if CDN fails
The purpose in pointing to a CDN is decidedly not to stay up-to-date. Generally, and as it appears in your example, you refer to a particular version of an external script. By doing so, you're protected against buggy releases and breaking changes.
The reason you link to the CDN is twofold: You take load off your own servers, so they can spend their time actually rendering dynamic output. And it decreases load times on your site.
See http://developer.yahoo.com/performance/rules.html#cdn
Regarding downtime: A file hosted on a CDN is far more likely to be available than a file hosted on your own network.