Web Performance optimization

Source: Internet
Author: User

High-performance JavaScript

1. Scripts

    Place script scripts at the bottom of the body tag ( </body>)

Synthesize multiple script scripts into one

Non-blocking load script ( deferred defer, dynamically created script, XMLHttpRequest script injection )   

2. Variables

    Reduce the search process for variables

    Use local variables as much as possible

Save object members, array elements, cross-scope variables as local variables

3.DOM

    Reduce the number of DOM accesses

    Using local variables to save a reference to the DOM

    Save the length of the HTML collection in a variable, copy the HTML collection into the array

    Use a faster API

    Watch Redraw and Reflow

    Use absolute positioning, out of document flow in animations

    Using event delegates to reduce the number of event processing

4. Algorithmic flow

    For, while, Do-while loop performance Similar

    Avoid using for-in loops, unless you want to traverse an object with an unknown number of attributes

    Reduce the number of iterations and the amount of iteration

Sometimes switch is faster than if else

Judging conditions more Use lookup tables, using arrays or ordinary objects to build a lookup table

5. String

    Array items connect in <=ie7 browser fast, recommend simple +,+= string connection

6. Browser thread, timer, web workers

    Any JavaScript task should not be executed for more than 100 milliseconds

    Timers can be used to delay code execution

    Web workers allows JavaScript to execute outside the UI thread

7. Data

    Reduce the number of HTTP requests, merging JavaScript, CSS.

    Shorten page load times, main content loaded, use AJAX to load secondary files

    Ensure that code errors are not output to the user, and the server processes

    Know when to use a mature library (framework) and when to write your own underlying AJAX code

8. Practice

    Avoid double evaluation of the eval () and the function constructor, giving the timer (settimeout,setinterval) the descendant function instead of the string

    Creating arrays, objects using direct amounts

    Avoid repetitive work, using lazy loading and conditional preloading

    Consider using bitwise operators when calculating math

    Try to use native methods

    Merge, compress, cache JavaScript files in server compression using CDN

Using network analysis tools, Profiling Tools

-----------------------------------------------------------

High Performance website building Guide (HTTP://STEVESOUDERS.COM/HPWS)

HTTP (Hyper Text Transfer Protocol Hypertext Transfer Protocol) is the Protocol by which browsers and servers communicate with each other over the Internet (consisting of requests and responses).

The browser sends an HTTP request to a specific URL, and the host server that corresponds to the URL returns an HTTP response.

A GET request contains a URL, a header. The HTTP response contains a status code, a header, and a response body.

HTTP is built on TCP (Transmission Control Protocol protocol).

1. Reduce HTTP requests

Image map: Associating multiple URLs on a single image

CSS Sprite: Integrate multiple background images into a large picture

Inline Picture:

Merging JavaScript and CSS files

2. using the Content Publishing Network (CDN)

A content publishing network is a set of Web servers that are distributed across multiple geographic locations for more efficient publishing of content to users.

Reduce response time, backup, extended storage, cache, and reduce peak traffic pressure

3. Add Expires Header

Web pages have a large number of components, and visitors make a lot of HTTP requests, through a long expires header, so that these components can be cached;

Let the browser cache pictures, styles, scripts

4. Compression Components

Reduces the size of the HTTP response file, reducing response time

Gzip compression is a popular compression method

5. Place the CSS style sheet at the top  

Avoid white screen and no style flicker use the link tag to place the stylesheet in the document head

6. Place JavaScript scripts at the bottom

Non-blocking load script parallel download

7. Avoid using CSS expressions (expression)   

. box{Width:expression (Document.body.clientWidth < 960? "960px": "Auto"); min-width:960px; }

8. using external JavaScript and CSS

9. Reduce DNS lookups

Caching DNS Lookups

Compress JavaScript code

Remove unnecessary characters, spaces, comments from your code

avoid redirects .

Redirection is rerouting a user from one URL to another

http://www.a.com/  //with slash (suggested plus) http://www.a.com   //No Slash

Remove Duplicate Scripts

configuring or removing an etag  

The entity label (ETAG) is a mechanism that Web servers and browsers use to confirm the validity of the caching component.

The etag is used to detect if the components in the browser cache match the components on the original server.

to make Ajax cacheable      

-----------------------------------------------------------

Advanced guidelines for high-performance website construction

1. Understanding Ajax Performance

2. Create a responsive web App

3. splitting and initializing the load

4. non-blocking load script

     XHR Eval

XHR Injection

Script in Iframe

Script Defer

document.write Script Tag

5. Consolidating Asynchronous Scripts

6. arrange in-line script 

7. write efficient JavaScript (high- performance JavaScript)

8. Scalable Comet

Simple polling

function Xhrrequest () {var xhr = new XMLHttpRequest (); xhr.onreadystatechange = function () {    if (xhr.readystate = = 4 &am p;& Xhr.status = =) {        //processing the returned data    }};xhr.open (' GET ', ' test.php ', true); Xhr.send (null);} SetTimeout (function () {xhrrequest ()},2000);

Long polling

function Longpoll (url,callback) {var xhr = new XMLHttpRequest (); xhr.onreadystatechange = function () {    if ( Xhr.readystate = = 4 && xhr.status = = $) {        callback (xhr.responsetext);        Send another request, reconnect the server        xhr.open (' GET ', url,true); Xhr.send (null);    }}; Xhr.open (' GET ', url,true); Xhr.send (null);}

9. Beyond gzip compression   

Using event delegates

Using relative URLs

Remove whitespace

Remove quotation marks from an attribute

Avoid inline styles

To set an alias for a JavaScript variable

Image Optimization    

Use the appropriate picture format

Optimize PNG images

Avoid using a Lphaimageloader

Using and optimizing CSS Sprites

Modular approach to building CSS sprites

Don't scale your pictures in HTML again

Cache pictures

One by one. dividing the primary domain

    Refresh the output of the document as early as possible

less iframe (inline frame)

The most expensive DOM element

Blocking onload Events

Simplified CSS Selectors   

CSS selectors are matched from right to left

Avoid using a wildcard rule

Do not qualify the ID selector

Do not qualify class selectors

Avoid using descendants, sub-selectors

Rely on inheritance

-----------------------------------------------------------

Web Performance optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.