search
HomeWeb Front-endJS TutorialHow to treat JavaScript_javascript tips to improve website performance

Especially for JavaScript files, when downloading it, parallel downloading is actually disabled and also blocks the rendering of the page!

About JavaScript downloading

When downloading a JavaScript script file, the browser will not start other downloads in parallel, but will allow the JavaScript script file to be downloaded separately before continuing with other requests. This will be a big problem for the overall performance of the page. The solution is as follows:

Solution 1: Inline the JavaScript script in the page, that is, write the JavaScript script directly in in HTML tags.
Advantages: Fastest. On the home page of a large website, it is reasonable to inline part of the JavaScript script directly in the HTML tag.
Disadvantages: JavaScript scripts are not cached separately, and other pages cannot share the JavaScript script (cannot be reused).

Solution 2: Place the link of the JavaScript script tag at the bottom of the HTML file tag.
Requirement: The script does not contain the document.write() method to rewrite the page.

Solution 3: Use a defferred script. That is, adding the defer attribute to the link tag indicates that the JS script does not contain the document.write() method.
Defect: After using the defferred script in Firefox, the JavaScript script will still block rendering and parallel downloading.
In IE, the effect is not obvious.

Summary: If a script can use deferred scripting technology, then it can definitely be moved to the bottom of the page!
That is, "Solution 3" can be completely replaced by "Solution 2".

Solution 4: Use load-after-download. That is, the JavaScript script is dynamically downloaded through the onload event after the page is loaded. (CSS is also universal)
Advantages: It does not block the rendering of HTML pages and enables the reuse of JavaScript scripts (the scripts will be cached in the browser).
Disadvantages: Additional JavaScript code is generated to implement this function, which increases program complexity.
Problem: Possibly loaded twice (once inline, once externally).

You can use IFrame to nest a page and load JavaScript scripts to solve this problem.
Example: http://stevesouders.com/hpws/post-onload.php

Solution 5: Dynamic inlining. Use cookies as indicators and use code to make judgments to inline external JS into the page.
Dynamic inlining is a further improvement of "download after load". It also increases the program complexity again.
Although JavaScript scripts are recommended to be placed at the bottom of the page, CSS style sheets should be placed at the top of the page!

About JavaScript minification

Minification is to remove unnecessary characters, comments, and whitespace from the code to reduce the size of JavaScript code, thereby improving the download length and loading of JavaScript speed.

Minimizing tool: JSMin JS Minifier js compression
JSMin is used to remove all unnecessary characters, comments, and whitespace in javascript files.

cmd Usage: C:Documents and Settingsxugang>jsmin js_rerurn.js
1. First specify the jsmin.exe folder
2. openWin.js is the source file
3. js_rerurn.js is the target file

Streamlining tool: ShrinkSafe (original name: Dojo Compressor) http://dojotoolkit.org/docs/shrinksafe
ShrinkSafe is used to remove whitespace in javascript files, and at the same time Variable names are also shortened by substitution.
cmd Usage: java -jar shrinksafe.jar infile.js > outfile.js
shrinksafe.jar is the tool name
infile.js is the source file
outfile.js is the target file

Note: When running in the console, make sure shrinksafe.jar and js.jar are in the same directory, and the input JS source file and output JS target file will also be in the same directory. (Default is in the root directory of drive C)

Generally, you can use the two tools JSMin and ShrinkSafe to streamline your JavaScript files.
Compress components

At the same time, don’t forget to compress scripts, style sheets and HTML documents through HTTP header declarations to reduce response time.
Browser client request: Accept-Encoding: gzip, deflate
Web server response: Content-Encoding: gzip
gzip is currently a popular and ideal and effective compression method, deflate is slightly less effective and less popular .

Statement
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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

提高网站性能的关键:PHP-FPM优化实操指南提高网站性能的关键:PHP-FPM优化实操指南Oct 05, 2023 am 09:28 AM

提高网站性能的关键:PHP-FPM优化实操指南随着互联网的迅猛发展,网站扮演着越来越重要的角色。对于网站运营者来说,提升网站的性能是至关重要的,它不仅能够提升用户体验,还能够提高搜索引擎的排名。而PHP-FPM(FastCGIProcessManager)作为PHP运行的进程管理器,对于提升网站性能起着至关重要的作用。本文将为大家提供PHP-FPM优化的

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

20+道必知必会的Vue面试题(附答案解析)20+道必知必会的Vue面试题(附答案解析)Apr 06, 2021 am 09:41 AM

本篇文章整理了20+Vue面试题分享给大家,同时附上答案解析。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

ES5和JavaScript是什么关系ES5和JavaScript是什么关系Apr 11, 2022 am 10:45 AM

ES5和JavaScript的关系是:ES5是JavaScript语言的国际标准,JavaScript是ES5的实现。ES5是ECMAScript基于JavaScript的规范标准的修正版本,规定了JavaScript的组成部分。

JavaScript详细解析之网络请求与远程资源JavaScript详细解析之网络请求与远程资源Apr 20, 2022 pm 06:39 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于网络请求与远程资源的相关问题,包括了跨源资源共享、预检请求、Fetch API等等内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)