Improve JavaScript program performance

Tram Ho

Javascipt is slowly becoming the dominant and the most popular programming language today, a premise for a series of front-end framwork, famous plugins that are slowly changing the world. Therefore, learning and understanding this language, only it helps programmers not be afraid of starvation in this competitive environment, when everyone knows how to do, the better will be the winner, it is possible. show through the results that the application they create gives the user experience. In today’s full text article, I will introduce some tools to improve the performance of a javascript program, hoping to help something in the process of working with your JS. (go)

1. Web worker

Web worker is javascript that runs in the background, without affecting the performance of the page.

As you know when executing a script on the HTML page, understand that the web page will be completely frozen until the script is finished, click, select, etc. So the web worker was born to help separate a certain flow of work without affecting the operation of the user, they can freely operate on the site while the other script is still running.

Using the web worker is quite simple:

First you need to check if the web worker is supported on your browser by:

First we need to initialize a worker with the purpose of running in the background named workder_file.js

Next, after having a simple background task, we create a web worker object.

The above code checks to see if the worker does not yet exist, then initialize the worker and specify the path to the worker to run, here is the file worker_file.js that is responsible for calculating and returning the output for us to display. go to the web. The display on the web we can do the following:

To create a running worker stop we can execute the command terminate it as follows:

So we can use web worker to completely separate the calculation and display, after calculating the web worker will return the result by postMessage (), the rest will be outside. Retrieve and display it on the page. Quite simple, huh ?

2. Deferred Scripts

Keep turning around this blocking-script, because it is the weakness that makes your website block when a script is processed, so we can also completely stop this execution until the html. The load is finished using the defer keyword when declaring a javascipt file

And in the process of downloading the js file it also does not block your website.

In the example above, we will show 3 times the alert, if the browser does not support defer, the execution order will be defer -> script -> load, while in the browser support defer, the order will be script-> defer- > load. The defer is executed before the onload event is fired.

3. Dynamic Script Elements

The DOM allows us to create dynamic HTML elements using javascript.

The code in file1.js will load when its element is added to the page. What is special about this type of load is that it does not block page handlers, but the problem with this technique is that it only works if the js file operates independently of any script. Any other, If this script is the interface used in a different script then all we need to do will be tracking to see when this file has finished loading. The job seems a bit more difficult.

4. XMLHttpRequest Script Injection

One of the nonblocking script approaches is to get js code through an XMLHttpRequest (XHR) object and then inject it into the page.

The above code will send a Get request to file1.js then use the onreadystatechange event to check readyStatus, and check the HTTP status code to see if it has success 200 or get a response from the cache with status 304. If all is verified an element New will be created and script will be executed. This way you can completely load a script without executing it.

5. Conclusion

Above are some ways to deal with javascipt to improve your website performance, although there are many difficulties but we can continue to learn to apply to our web application. Hope to bring you better results in improving your work skills as well as performance and find the best measures suitable for your application. ?

Share the news now

Source : Viblo