37
JavaScript loading techniques & Performance
Adding external script files to your HTML document is simple that you could do it in your sleep.
But this is not as trivial as you think. Where and how you add your script file majorly influences the performance of your website.
In this post, we will go through the techniques to include external script files to your HTML and look at how this can affect the performance.
We will compare which technique is preferable and efficient over others in varying situations.
This blog post assumes you are familiar with basic HTML, CSS and JavaScript syntax.
We will also learn about the attributes:
We will also learn about the attributes:
async
and defer
.As you might already know, external JavaScript files can be included in the:
- head
- body
Before we continue and discuss about these techniques in depth, let's understand what happens when a browser loads a webpage.
Now enough with all this! Let's get to the actual post!!
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
</head>
<body>
<!-- DOCUMENT CONTENT -->
<script src="./src/main.js"></script>
</body>
</html>
<script>
within the HTML body.

How do we manage to load JavaScript files, and at the same time retain user experience and optimize website performance?
The simple answer to this is: Add script references inside the head

<head>
.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js"></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
<head>
, the script files are fetched before the HTML DOM is parsed and loaded completely. <p>
when user clicks the button. Look what happens when you add the script source in the <head>
.
You get an error "cannot read property addEventListener of null". This happens because the DOM is loaded after JavaScript is fetched, and hence there is no reference to the button.
document.addEventListener('DOMContentLoaded', function() {
btn.addEventListener('click', () => {
p.textContent = "You clicked me!";
});
});
And now if user clicks the buton, there is no error:
This is yet again an old technique.
HTML5 provides two new, modern features that prevents blocking of HTML parse and JavaScript load.
The two attributes: async
and (or) defer
are added to the script tag when it is included in the <head>
.
Both the attributes ask the browser to load the script file in a separate thread without blocking the HTML file from being parsed.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js" async></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
- Script fetching, loading and execution are independent of each other. That is code in one script does not affect code in another.
- When you need scripts to perform initialization tasks that are required before the actual execution begins.
- When you have scripts that do not manipulate the DOM.
<head>
<!-- Add script file source here -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" async></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous" async></script>
<script src="./src/init.js" async></script>
</head>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript reference inside body</title>
<!-- Add script file source here -->
<script src="./src/main.js" defer></script>
</head>
<body>
<!-- DOCUMENT CONTENT -->
</body>
</html>
async
, script is not executed immediately once the file is loaded, and the DOM load is not blocked. <head>
.- The script files in your web page are dependent on each other, and the execution of one script affects the other.
- When your script manipulates the DOM content.
<head>
<!-- Add script file source here -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" defer></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous" defer></script>
<script src="./src/main.js" defer></script>
</head>
<body>
only if the script your website uses is minimal.If you have multiple scripts that are heavy, refer to it within the <head>
as sourcing within the <body>
blocks JavaScript from loading, thereby affecting the performance of your website.
Use async in case the scripts in your website are independent of each other, and you want to execute code before the main JavaScript loads.
Use defer when you have scripts that rely on parsing of HTML and manipulation of DOM elements.

Thank you so much for your support and reading this blog post.
Help me out by sharing this to your friends, and comment down what you felt about this post.
Help me out by sharing this to your friends, and comment down what you felt about this post.
Do heart, save, unicorn, or do all of it if you enjoyed and learned rom this post!
37