Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do multiple inline JS scripts slow down loading time of a page?

I'm trying to optimize the loading time of a site that has a lot of JS spaghetti code. Some of it actually looks like this:

<script>var x="foo";</script>
<script>var y="bar";</script>

Instead of the sane-programmer code:

<script>
    var x="foo";
    var y="bar";
</script>

So I was wondering whether this kind of thing actually does harm? Aside from the aesthetics, would combining the scripts into a single script tag bring any loading time benefit?

like image 911
Confuzed Avatar asked Jan 31 '11 13:01

Confuzed


People also ask

Are inline script tags bad?

Note: Using Inline JavaScript is a bad practice and it is not recommended. It can be used for demonstration purposes so that the demonstrator doesn't have to deal with 2 separate files at a time. It is recommended to write JavaScript code in a separate .

What's wrong with inline JavaScript?

Having inline js on different pages will make it difficult to maintain for you and others as the project scale increases. Moreover using separate js files you can encourage reusability and modular code design. keeps your html clean and you know where to look when any js error occurs instead of multiple templates.

Is inline JavaScript faster?

In Raw Terms, Inline Is Faster Although the total amount of data downloaded is the same, the inline example is 30–50% faster than the external example.


2 Answers

When a browser encounters a script element during the parsing of a page's HTML the following happens:

  1. The browser pauses processing of the HTML.
  2. The browser initiates an instance of its JavaScript interpreter.
  3. The JavaScript interpreter compiles the JavaScript and adds it to the current page's global JavaScript pool.
  4. The browser continues parsing the HTML, repeating 2. and 3. as many times as there are script elements in the HTML.

So, having multiple script elements will slow down rendering of the page. The fewer script elements present in the HTML the faster the page will render.

If your document is including scripts via the src attrbute (e.g. <script src="http://example.com/myscript.js">) then you can use the async attribute to delay script processing. This is a feature supported in newer browsers (e.g. Firefox 3.6 and Webkit based browsers released since September 2010). But inlined scripts will still block parsing until they have been interpreted.

like image 156
James Sumners Avatar answered Oct 30 '22 19:10

James Sumners


Insofar as there are more bytes to download, the first example is slower.

However, I'm certain that you will never be able to differentiate between the negligible micro-optimization of the latter example and the former example. Other than organization, there's no reason to bother.

That said, for the sake of organization, I would definitely bother! Keeping your code organized will speed development time. Keeping all of your JavaScript in one place will prevent you from running around searching for a snippet.

I would strive to move as much code first into an external file, and anything else into the foot of your document.

  1. You will garner the most speed benefit by using an external file (due to browser caching) than anything else.
  2. You will allow your markup to be rendered by the browser before downloading or executing your JavaScript if you move the <script> tags to the footer of the page. This will create the illusion of a faster download.

Incidentally, (and just as trivially) the most optimized version of your script (aside from using an external file) would be:

<script>
    var x='foo', y='bar';
</script>
like image 42
Stephen Avatar answered Oct 30 '22 20:10

Stephen