How to Remove Unused JavaScript and defer loading scripts ?

How to Remove Unused JavaScript and defer loading scripts ?

How to Reduce unused JavaScript and defer loading scripts until they are required to decrease bytes consumed by network activity ?

Eliminate unused JavaScript: Start by identifying and removing any JavaScript code that is not being used on your website. This could include scripts that were added for previous features or functionality that are no longer in use. Removing unused JavaScript reduces the overall file size and decreases the network activity.

·         Split scripts into smaller modules:

Instead of having a single large JavaScript file, consider breaking it down into smaller modules based on functionality. This allows you to load only the required modules when needed, reducing the initial loading time.

·         Asynchronous and deferred loading:

Use the "async" or "defer" attributes when loading external JavaScript files. These attributes control how scripts are fetched and executed by the browser.

·         Async attribute:

Adding the "async" attribute to a script tag allows the browser to continue parsing the HTML document while the script is being fetched. Once the script is downloaded, it will be executed, which may occur out of order with respect to other elements on the page. This is suitable for scripts that do not depend on the DOM or other scripts.

Example: <script src="script.js" async></script>

                                                   or,

                <script async='async' src='script.js'/>

·         Defer attribute:

Adding the "defer" attribute to a script tag tells the browser to defer the execution of the script until the HTML document has been parsed. Scripts with the defer attribute will be executed in the order they appear in the HTML document. This is suitable for scripts that rely on the DOM or need to maintain execution order.

Example: <script src="script.js" defer></script>

                                   or,

                <script defer='defer' src='script.js'/>

·         Load scripts dynamically:

Instead of including all scripts in the HTML markup, you can load scripts dynamically when they are needed. This can be done using JavaScript techniques such as the createElement and appendChild methods to create and insert script elements programmatically.

·         Lazy loading:

For scripts that are not critical for the initial page load, you can implement lazy loading techniques. Lazy loading defers the loading of scripts until they are needed, typically triggered by user interactions or specific conditions. This can be achieved using libraries or custom JavaScript code.

By implementing these practices, you can optimize the loading of JavaScript files, reduce network activity, and improve the overall performance of your website.


Online Source

Comments

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources