Skip to content Skip to sidebar Skip to footer

Javascript. Does The Position Of The Code Affect Performance?

I was searching for ways and tricks to improve my javascript code performance. I would like to know what are the best practice to use in order to have best performance. Does the po

Solution 1:

If there is a lot of JS, put it at the end of the document. Although this makes no difference on load time, the user will see the page sooner and can begin to read it while your js loads, as opposed to seeing nothing until your JS is downloaded (which is what happens when you put it in the head). It merely makes the download appear faster. This would also solve the problem mentioned above about the script executing on an unfinished document, although for that, an even better solution is to use window.onload().

Solution 2:

I don't think it has performance impact but it can have logic impact. If your javascript is not deferred and it tries to use a dom element declared after the script, the script will not run.

Solution 3:

i dont think so that the position of the code will affect the performance because whenever a jsp is loaded its with all its content and script so the complete code is already present in the browser, and it wont matter where your script lies in the code.

Solution 4:

Speaking without actually testing, and for any performance discussion that's dangerous, I find it hard to imagine that it will have any significant impact.

Once the code has been parsed then it's location is surely irrelevant? So the only thing that it could impact is initialisation. However I can't see that position within an HTML file can be significant, the whole file would need to be interpreted by the browser.

Solution 5:

You can place the JavaScript, anywhere within your web page. Also best practice is to load the JS at the end

Post a Comment for "Javascript. Does The Position Of The Code Affect Performance?"