JS Resource Loading optimization

Source: Internet
Author: User

In the application of Internet or large amount of access, the optimization of JS loading is not limited. Several optimization methods are documented below

CDN + Browser Cache

CDN (Content Delivery Network) is the most traditional way to optimize the contents distribution networks. In fact, the page depends on the JS (static) placed on the CDN, or use some CDN library, in order to reduce the application server requests, and the browser cache is not repeatedly loaded JS file nature.

Advantages:

1. Simple and easy to maintain

2.304 Cache

Simply to turn off the request, the cache does not reload.

Disadvantages:

1. The cache will be invalidated and will be requested when the user forces a refresh

2. Unable to incrementally update

Offline storage (HTML5 AppCache)

Offline caching by configuring a manifest file

The manifest file contains:

1. Current version number

2.cache file

3. Files that require network requests

4. Failure response

Advantages:

1. There is a version update before the request (CDN mode will still have a request, just 304 requests)

Disadvantages:

1. After the version is updated, the page must be refreshed once to enable the new version of the file.

PS: So you have to listen to an event and re-swipe the page

function (e) {        console.warn (' page update ');        Window.applicationCache.update ();        Window.applicationCache.swapCache ();        SetTimeout (function  () {            location.reload ();         false); From  http://dd.m.taobao.com/

2. Go to the Offline storage page, if you do not update the version, it will be the original static page is not requested, so that the gray-scale publishing and other policies.

PS: Grayscale Publishing: The simple part is that some people use the old version, some people use the new version.

3. Unable to incrementally update

PS: For example a 100KB file, you have only modified the contents of 4KB, but you have to update the entire 100KB file.

Local storage

The application page compares the user's current version to the server version (local storage has version information), through the cross-domain AJAX request has updated JS file, if it is an incremental update, the algorithm calculates a new version of the JS code, The code is then eval and the new JS string is stored in the local store (localstorage) for the next incremental update.

Advantages:

1. Reduce unnecessary requests

2. Avoid the problem of offline storage

3. Incremental update (requires algorithm calculation)

The ps:1.chunk algorithm, four characters for a block, is compared to a block.

2. Editing distance calculation algorithm

Disadvantages:

1. Implementation is more complex than the previous methods

2. Some browsers do not support

JS Resource Loading 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.