JS website performance optimization notes_PHP tutorial

WBOY
Release: 2016-07-21 15:29:14
Original
854 people have browsed it

1. Remove JavaScript comments

Except for comments, all other // or /* */ comments can be safely removed as they have no meaning to the end user.

2. Remove white space in JavaScript

For example: x = x + 1; can be written simply as: x=x+1; .

3. Code optimization

Simple methods such as removing implied semicolons, variable declarations in certain situations, or empty carriage return statements can further reduce script code. Some short expressions will also produce good optimizations, such as:

x=x+1;

can be written as:

x++;

But you have to be careful, otherwise the code is prone to errors.

4. Rename user-defined variables and functions

For the convenience of reading, we all know that variables like sumTotal should be used instead of s in scripts. However, considering the download speed, sumTotal this variable seems verbose. This length is meaningless to the end user, but it is a burden for the browser to download. At this time s becomes the better choice. Write code that is easy to read first, and then use some tools to process it for delivery. This approach once again demonstrates its value here. Reverting all names to one or two letters would bring significant improvements.

5. Rewrite built-in objects

Long user variable names will cause JavaScript code to be too long. In addition, built-in objects (such as Window, Document, Navigator, etc.) are also one of the reasons. For example:

alert(window.navigator.appName);
alert(window.navigator.appVersion);
alert(window.navigator.userAgent);

can be rewritten into the following short code:

w=window;n=w.navigator;a=alert;
a(n.appName);
a(n.appVersion);
a(n.userAgent);

If these objects are used frequently, the benefits of rewriting in this way are self-evident. In fact, these objects are often called. However, I would like to remind you that if the Window or Navigator object is only used once, such replacement will make the code longer. This technique brings about a problem of script execution efficiency after object renaming: in addition to the benefits brought by code length, this rewriting and renaming will actually slightly improve the script execution speed, because these objects will be placed in all objects. The earlier position in the calling object. JavaScript game developers have been using this technique for years to increase download and execution speeds and reduce local browser memory usage, killing three birds with one stone.

6. Refactor the <script> and <style> calling methods to optimize the number of requests </b></p> <p align="left">We often see code marked like this in the header of an HTML file: </p> <p align="left"><script src="/scripts/rollovers.js"></script>

< ;script src="/scripts/tracking.js">

In most cases, the above code should be simplified to:

G.js contains all functions for global use. While splitting the script file into three makes sense for maintenance purposes, it doesn't make sense for code transfer. A single script download is much more efficient than three separate requests, and it also reduces the length of the markup code.

7. Merge your javascript files

Reduce the number of HTTP Request requests as much as possible.

8. Place the script at the bottom of the web page

    Scripts are generally used for user interaction. So if the page hasn’t come out yet and users don’t even know what the page looks like, then talking about interaction is just nonsense. So, scripts are just the opposite of CSS, scripts should be placed at the bottom of the page.

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323483.htmlTechArticle1. Remove JavaScript comments Except for comments, all other // or /* */ comments can be safely deleted. Because they don't mean anything to the end user. 2. Remove JavaScript...
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template