- the first step you can do right away is to put the js reference at the end of the body to speed up the presentation of the entire page.
- if the JS file is very large, it will be loaded into several files according to functionality and dependencies.
- if the content in that js file is not necessary for every page, it is recommended to load it on demand. You can use requirejs .
provides several quick solutions:
- compress the code as much as possible without generating map
- nginx enables gzip, to improve a lot
- upload to cdn, such as Qiniuyun
- optimize code structure to load on demand
- split the common code and the business code. The common code can not be updated later. Use the http cache
many of the solutions mentioned above can be solved using webpack, gulp, and so on, depending on what your project is like.
webpack enable compression, as shown in figure
- npm install-- save-dev compression-webpack-plugin
- productionGzip: false, false changed to true
can you ask a bigger question?
it's like telling the doctor that you're not feeling well and asking the doctor to give you a prescription.
Let me tell you a little bit about the way of thinking when you encounter this kind of problem.
first of all, your problem should be that loads this page very slowly for the first time
.
so what is the reason for the slow loading speed?
to know why the loading speed is slow, you need to know how browsers render web pages.
Let me briefly say that the browser is generally divided into two processes from the beginning to the rendering:
- request HTML
- parsing HTML, if it encounters CSS or JS (regardless of asynchronous scripts), it will wait for it to download and parse
so consider optimization in terms of download speed and parsing speed.
to improve download speed, it is easy to think of dividing a large file into multiple small files. But this optimization is not always successful, and it is not difficult to understand why it can improve performance.
improving parsing speed is also easy to think of not writing complex JS operations, or putting complex operations into RAF or RIC. But it doesn't matter.
what matters is that you know how it works and how to make choices in different situations
For more information, please see my article on front-end performance optimization. Address:
https://github.com/azl3979858.