---
title: "How to speed up the website with my JavaScript code in the server?"  
description: "How to speed up the website with my JavaScript code in the server?"  
author: "Anonymous User"  
published: 2021-07-20  
updated: 2021-07-20  
canonical: https://www.mindstick.com/forum/156531/how-to-speed-up-the-website-with-my-javascript-code-in-the-server  
category: "javascript"  
tags: ["javascript", "web application"]  
reading_time: 3 minutes  

---

# How to speed up the website with my JavaScript code in the server?

How to do the [fast](https://www.mindstick.com/articles/12289/build-your-store-the-fast-way-with-magento-2-frontend-builder) [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of the [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) in a [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)? What things we need to keep in mind before deploying my code in a server?

## Replies

### Reply by Anonymous User

Fast execution of JavaScript should be important for the developers as well as the for the user. So the things we should keep in mind before writing JavaScript code are as follows:

1.) Reduce Activity in Loops

Loops are the basics which are used in programming. So before each statement in a loop, including the for statement, is executed for each iteration of the loop.

Statements or assignments that can be placed outside the loop will make the loop run faster.

Bad way to write code :

```
for (let i = 0; i < arr.length; i++) {
```

Better way to write above code:

```
let len = arr.length;
for (let i = 0; i < len; i++) {}
```

2.)Reduce DOM Access

Accessing HTML DOM is very slow compared to other JavaScript statements.

If you expect to access a DOM element multiple times, access it once, and use it as a local variable:

```
const obj = document.getElementById('selector');
obj.innerHTML = 'Hello World!';
```

3.) Reduce DOM Size

The number of HTML elements should be minimal without any requirement:

This will always improve page loading, and speed up rendering (page display), especially on smaller devices.

Every attempt to search the DOM (such as getElementsByTagName) would benefit from a smaller DOM.

4.) Avoid Unnecessary Variables

Do not create a unnecessary variables if you don't need it in the document

Often you can replace code like this:

```
let fullName = firstName + ' ' + lastName;
document.getElementById('demo').innerHTML = fullName;
```

With this:

```
document.getElementById('demo').innerHTML = firstName + ' ' + lastName;
```

5.) Delay JavaScript Loading

Placing your script at the bottom of the page body allows the browser to load the page first.

While one script is being downloaded, the browser will not initiate any other downloads. Also all parsing and rendering activity can be blocked.

The HTTP specification defines that browsers should not download more than two components in parallel.One option is to use defer='true' in the script tag. The defer attribute specifies that the script should be executed after the page is parsed, but this only works for external scripts.

If possible, you can add your script after the page is loaded:

Example

```
<script>
window.onload = function() {
  const element = document.createElement('script');
  element.src = 'myScript.js';
  document.body.appendChild(element);
};
</script>
```

Hope this information will be helpful for you in enhancing the performance of your JavaScript code.

Happy Coding!


---

Original Source: https://www.mindstick.com/forum/156531/how-to-speed-up-the-website-with-my-javascript-code-in-the-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
