Home > Web Front-end > JS Tutorial > Accelerate Extjs (javascript acceleration)_extjs

Accelerate Extjs (javascript acceleration)_extjs

WBOY
Release: 2016-05-16 18:21:05
Original
1122 people have browsed it

All js in EXT are relatively large. One ext-all-debug.js is more than 2MB, and its compressed version (removing line breaks and spaces in js) is also more than 600K, which is suitable for those who are not using a very fast network. When the time comes, you have to wait a long time to download js. Among them, the calendar task control has as many as four or five js, and the size of each js reaches more than 70K. Although we adopt the post-loading method, the js is only downloaded when the user clicks the My Task function, but this is still very complicated. Slow, because the downloaded js is very slow
. In view of this, the speed of using programs like Joffice on the Internet will make many developers afraid to choose ext as a development technology.

We can improve the running speed of the application through the following methods:
1. Load js as little as possible in the early stage.
This is better used in Joffice, using ScriptMgr.load method is used to complete it. After the loading is completed, it will insert a div into the body. As long as the current page is not refreshed, the next time you access this function, there is no need to load js

Copy code The code is as follows:

function $ImportJs(viewName,callback) {
var b = document.getElementById(viewName '- hiden');
if (b != null) {
var view = eval('new ' viewName '()');
callback.call(this, view);
} else {
var jsArr = eval('App.importJs.' viewName);
if(jsArr==undefined){
var view = eval('new ' viewName '()');
callback.call(this, view);
return ;
}
ScriptMgr.load({
scripts : jsArr,
callback : function() {
Ext.DomHelper.append (document.body,"");
var view = eval(' new ' viewName '()');
callback.call(this, view);
}
});
}

2. Use Gzip for js Super powerful compression
The official website of Gzip is:
http://www.gnu.org/software/gzip/
Gzip is very simple to use
Extract to a directory and you will see There is a Gzip.exe file, then enter the directory in the command window, execute
gzip ext-all.js
ext-all.js will immediately become ext-all.js.gz
The size will change from the original More than 600k suddenly turned into more than 160k, which is much smaller. This time the download speed is very fast.
So can the browser parse this kind of compressed file? The answer is yes, the premise is to tell the browser that this kind of file needs to be decompressed and then executed. The decompression process is performed by the browser.
So how does the application tell the browser that the file needs to be decompressed? This must be done by the server through the Http Header command.
In JOffice, it is done through Filter.
1. Change the name of the ext.all.js.gz file to ext.all.gzjs. The Filter will intercept access to this file after a while.
2. Write a Filter and add instructions to the Header
The code is as follows:
Copy the code The code is as follows:

package com.htsoft.core.web.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GzipJsFilter implements Filter {
Map headers = new HashMap();
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if(req instanceof HttpServletRequest) {
doFilter((HttpServletRequest)req, (HttpServletResponse)res, chain);
}else {
chain.doFilter(req, res);
}
}
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
for( Iterator it = headers.entrySet().iterator();it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
response.addHeader((String) entry.getKey(),(String)entry.getValue());
}
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
String headersStr = config.getInitParameter("headers");
String[] headers = headersStr.split(",");
for(int i = 0; i < headers.length; i ) {
String[] temp = headers[i].split("=");
this.headers.put(temp[0].trim(), temp[1].trim());
}
}
}

3. In the WEB.xml file, add the following configuration:
Copy code The code is as follows:


GzipJsFilter
com.htsoft.core.web.filter.GzipJsFilter

headers
Content-Encoding=gzip



GzipJsFilter
* .gzjs
lt;/filter-mapping>


4. Introduce the compressed file into index.jsp:

Accelerate Extjs (javascript acceleration)_extjs

You can see that after the browser decompresses it, the code is the same:

Accelerate Extjs (javascript acceleration)_extjs

As you can see above, this part is used on the external network, and its speed is relatively fast. Of course, it will take some time for the browser to decompress this file, but decompressing it locally is very fast, so you can leave it alone.

Although it cannot completely solve the speed problem, it can still help.

Related labels:
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