Anyway we'll focus in that front end that takes around 80% of our time when browsing the web. There are 2 main things to focus on, one is the amount of request and the other the size of the response.
In the first part we focused into the amount and time spend for the http request. So in this one we'll see how to optimize the size of a request. In order to do that we'll:
- Components IDs:
The id of the components, when parsed for Ajax4jsf if are not present they’ll be assigned by the parser automatically which doesn’t allow using selenium for testing in the page and the difficulty for reading for maintenance of the application is increased.
Because of it is highly recommended to assign manually the IDs to the components, but since the Ajax parser will join all the IDs using a semicolon to differentiate them is an appropriate approach to try to keep the IDs as short as possible so the size of the page is kept small. - Context Root
The same applies to the view IDs, we need to set in the jboss-web.xml the context-root parameter so we keep it as short as possible. That way we call the app straight, for instance we can leave like <context-root>/</context-root> so we don’t put anything before the name of the page. - A4J Support
Try to use a4j:support instead of the already provided a4j commandButtons and commandLinks. The code is re-usable and the amount of code generated by the javascript library for that component is slightly smaller than the one used for the other ones. - Gzip components
When transferring HTTP request through the network, the time it takes to transfer all the data seems usually out of our control, since it relies on bandwidth. Anyway there is a thing we can do to improve the response, and is compress the components we are about to sent through the network.
Starting with HTTP/1.1, web clients indicate support for compression with the Accept-Encoding header in the HTTP request, i.e: Accept-Encoding: gzip, deflate. If the web server sees this header in the request, it may compress the response using one of the methods listed by the client. The web server notifies the web client of this via the Content-Encoding header in the response, i.e: Content-Encoding: gzip.
For the Jboss AS to enable the gzip compression we have to modify the file deploy/jbossweb.sar/server.xml , look for this piece of code:<Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" connectionTimeout="20000" redirectPort="8443" />
And change it for:<Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" compression="on"
compressableMimeType="text/html,text/xml,text/css,text/javascript, application/x-javascript,application/javascript"
connectionTimeout="20000" redirectPort="8443"
/> - Improve your CSS and Javascript files
A common way to ensure that the request are the minimum as possible when developing with technologies different than Richfaces is merge all the stylesheet and script into single files, so we only have one file for CSS and one for Javascript. This way we only have to send 2 request at the beginning and the browsers will caches all the information only the first time the page is loaded.
In RichFaces anyway we can pack all the JavaScript and CSS in its component set and deliver them in two requests on the very first page in the application and ensure that the browser caches them on all future requests. To enable this feature, add the following two context parameters to the /WEB-INF/web.xml file:<context-param> <param-name>org.richfaces.LoadStyleStrategy</param-name> <param-value>ALL</param-value> </context-param>
<context-param> <param-name>org.richfaces.LoadScriptStrategy</param-name> <param-value>ALL
Because of this approach is important to avoid CSS expressions and try to minify the Javascript and CSS. CSS expressions are the ones of typebackground-color:expression((new Date()).getHours()%2 ? "#B8D4FF":"#F08A00");
These expressions were largely used while IE5 was released, since IE8 has been deprecated due to massive cost of rendering time, the expression was evaluated once and another every time the page was rendered, resized, scrolled or even when the user moves over the page. The expressions where anyway not possible to use with other browsers, so try to avoid them as much as possible.
Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this improves response time performance because the size of the downloaded file is reduced. A tool that can be used for minification of Javascript along with CSS is YUI Compressor from Yahoo!
Once all this is done, remember to put the stylesheet in the top of the document (template) and the Javascript at the bottom to ensure that they are loaded when it’s intended so we avoid the typical “blank” page while loading. Remember that in IE @import behaves the same as using <link> at the bottom of the page, so it's best not to use it.
Last thing to keep in mind is try to keep the path to the css and javascript files as small as possible, anyway it is possible to reduce the size of the css including it into a gzip file, when including this file as css-stylesheet the resource will be unpacked - Reduce complexity of the page
Reduce the complexity of the page is good not only for the speed of the page but also for the reutilization of code and maintainability of the application. The easier the better is the approach that we might use here.
A complex page means more bytes to download and it also means slower DOM access in JavaScript. It makes a difference if you loop through 500 or 5000 DOM elements on the page when you want to add an event handler for example.
A good approach for that is to re-use as much pieces of code as possible, this way we can optimize the number of elements rendered in one piece of code and have an impact in the whole web applications. Such pieces can be menus, filters, layouts, tables’ headers and bodies…
Also avoid fixing layout with JavaScript since accessing DOM elements with JavaScript is extremely slow. - Keep cookie’s size small
It's important to keep the size of cookies as low as possible to minimize the impact on the user's response time.
Opposite with the cache, in the cookies, to add an early expires date or none, removes the cookie sooner, improving the user response time. - Optimize images
First of all, avoid using GIFs when possible, anything a GIF can do, a PNG can do (only animations are the difference), besides if the gif is not optimized to use the palette corresponding to the amount of colors used in the image there is some space wasted. Also, GIFs images are usually bigger than the PNG images.
If Possible use either PNG or JPEGs, for the first one, try to run some optimization program like pngcrushjust in case there is some possible space for optimization.
Please, don’t scale images in HTML, use an appropriate size of the image for each picture.
Try to use CSS sprites, in order to do that follow this tutorial