Sunday, December 23, 2012

Website Performance Tips: CSS Improvement


It is weird to hear the CSS can also impact on website performance. But it is true, badly written CSS or unorganized CSS selectors can impact the site performance.

This post will help you to reduce CSS file size so that it can get downloaded faster by browser and also help browser to render HTML element effectively to reduce page load time.


Selector Usage
The organization and usage of selector impact the performance. 
The order of CSS selector by their usage efficiency is

  • ID, e.g. #cp-r
  • Class, e.g. .sel
  • Type, e.g. div
  • Adjacent sibling, e.g.  div + span
  • Child, e.g. li > ul
  • Descendant, e.g. li a
  • Attribute, e.g. [type="input"]
  • Pseudo-classes/-elements, e.g. a:hover
By this order ID is more efficient selector then class, but practically there is not much difference between ID and class selector but organization and usage of selectors is important.

Suppose you have a webpage with number of paragraphs
and there is one div with ID as "cp-r” for a Article and this Article div has two paragraphs now we want to apply style to Article’s paragraphs. therefor we can write CSS style as

#cp-r p{...} 

This works fine but this kind of selector spent more time on rendering the style for Article's paragraphs.

Problem is that the browser reads selector from right to left. Therefore it reads the paragraph first rather than ID, that is why it access all paragraph first then look for its parent.

To get complete usage of selector we will add common class name to Article’s paragraphs
Like 

<div id='cp-r'>
  <p class='cp-p'> ... </p>
  <p class='cp-p'> ... </p>
</div>


And in CSS we will write selector as

#cp-r .cp-p{...}

What will it do? Browser will search only ".cp-p" class selector first, in this case it is only two times.

Over qualifying selectors also impact the performance and always try to reduce over qualifying selectors. The over qualifying selectors look like this

body div #cp-r p{…}

Here there is no use of body and div selector because all of the work end at ID “cp-r”, this kind of style force browser to find all div elements of body.

Always try to find selector which won’t force browser to do unnecessary work.

Put CSS inside HEAD
Adding CSS inside HEAD will improve the webpage performance. This allow browser to render page progressively .browser renders contents as soon as it fetch it from network. it gives user a feedback that contents are appearing in browser.
If CSS is at bottom of HTML then browser wont render the html with given style until it fetch all contents and user see the plan HTML and after browser downloads the CSS it flashes the HTML with style which would create a bad impression for user.


Selector Names
There is much difference between server side programming and client side programming. such as variable names, for server side programming I can write a variable name for a customer profile object as

$customer_profile_record 

but for client side like in CSS I would like to write variable name as

.cp-r {} (in css)
<div class=”cp-r”> (in HTML)

The reason behind this kind of naming convention is that the variable name at server side should be descriptive so that other team members can understand it when they refer it.

But on client side (CSS, HTML) it should not be as such descriptive because a long and descriptive variable (CSS selector) name can increase the CSS file size.

Therefore CSS selector name should be short. For reference you can inspect Google website for CSS selectors name.
Reduce redundancy
There will many cases you will find style is getting repeated. 
Consider following style

.parent { width: 200px; height: 100px}
.child-1 { width: 200px; height: 100px}
.child-2 { width: 200px; height: 100px}

Here parent and child elements are having same style.

It can be reduced by adding a common class 

 .wrapper{ width: 200px; height: 100px}

and can use it in HTML as

<div class='wrapper'> <!-- parent -->
  <div class='wrapper'><!-- child -->
  </div>
  <div class='wrapper'><!-- child -->
  </div>
</div>

In one more case you can find the redundancy, when image sprite is used for different HTML element.
in this case same background image is used to different HTML elements 

.element-1 { background-image: url('image-sprite') ... }
.element-2 { background-image: url('image-sprite') ... }
.element-3 { background-image: url('image-sprite') ... }
.element-4 { background-image: url('image-sprite') ... }
.element-5 { background-image: url('image-sprite') ... }

it is unnecessarily increasing the CSS file size , it can be reduced by having common background image stlye and use it in each required HTML elements. 
like

.sprite { background-image: url('image-sprite') ... }

In HTML

<div class='sprite '>
</div>
....
<div class='sprite '>
</div>
....
<div class='sprite '>
</div>

There would be much other case where you will find the redundancy. Therefore try to minimize redundancy and decrees the CSS file size.

Remove unnecessary CSS code
Remove unnecessary CSS code which is not going to use for requested webpage.
Some time we keep a common CSS file to whole website like 'main.css' and add each style in it. Problem with this is, it increases the CSS file size. Homepage does not need whole website style, it needs only required style.

Or sometime we create different CSS files for each module of a website, but include them inside homepage. Which will increase the page load time because browser try to download the unused CSS files.


Avoid CSS expression
CSS expression is useful for adding style dynamically but they are evaluated more often as we expect. Expression can evaluate on page re-size, scroll or mouse move event.
It is better to use jQuery to add dynamic CSS style rather than CSS expression.

Minified CSS
Always try to minify the CSS file. it will help to remove unnecessary CSS code and improve the page load time.
There are many tools available online to minify the CSS file. like  YUI compressor.



Try to reduce the CSS file size and use selector effectively.
Hope this post would help to increase website performance.




Thursday, December 13, 2012

How Google search engine works



It feels good when you see your website appear in Google search results. But some time you crack your head but still can't see your website in Google search results. Therefore you should know it first that how Google create search results and what are the factors that affect the Google search results.

Before we proceed, have you ever imagine how Google searches for particular query. And what happens when you type something on Google search box?

When you type something on Google search box and do search, then you are actually searching for Google Index of web pages or information that Google have.

What is the Google Index?

Google indexes are pages of millions of website stored across multiple machines.

Now you will think how Google has these webpage inside its storage.

Google has software called as Spider.
Spider start from fetching few pages then it follows the links they point to. Then visit those pages and follow the links they link to, then visit to those pages until it index a big chunk of webpages.


Note: allow Google to crawl your website by allowing access to Googlebot from robot.txt

Whenever you search for particular keyword, Google searches that keyword inside the Google Index.
Google fetches thousand of index page Then Google filter out the most relevant index pages for search result. And display the search result that you are looking for.

How does Google searches for relevant index pages?


Google asks questions to each fetched index pages, some of the questions are listed below.


  • How many time the searched keyword appear in index page.
  • Is the searched keyword appearing in title?
  • Is the searched keyword appearing in link tag?
  • Is the searched keyword appearing inside the meta description tag.
  • Or Index page has synonym of the searched keyword.
  • After that Google see how many Backlinks are present for this index page.
  • What is Pagerank for index page?


Note:   followings are best practices that make your website index by Google crawler.
  • You must have at least one Title or h1 tag to make Google know it as title of your webpage.
  • Use "alt" attribute to describe the link tag of your webpage.
  • Always use description Meta tag to show or describe the webpage.

  

There are few term that I used to explain the Google search mechanism. Let discuss those terms.

What are Backlinks?


Backlinks are incoming links to your website or some other websites or webpages are having link to your website.

Like ABC is website which has a link of XYZ website in its webpage, therefore XYZ is having a Backlinks.

Backlinks are very important for creating healthy Pagerank.

Number of Backlinks means higher percentage to make your website appear at higher position in search result.

Now you will think that if I add as many Backlinks to my website then my website will come at higher place in search results.

Hmm, it is not true. Numbers of backlinks are good for Pagerank but the Qualities of Backlinks are also important. The reputed Backlinks outscore more than spam or unimportant Backlinks.


Let say you’re having ABC website with 100 Backlinks and I have a website with 10 Backlinks, but your website's Backlinks are from spam or lower rated websites and Backlinks of my website are from reputed site like Yahoo NewsNew York Times then my website will come above of your website or may be at first place.
Therefore quality of Backlinks decide the position of websites in search result.


Now we move to next point.

What is Pagerank?


In Wikipedia it is given as "PageRank is a link analysis algorithm, named after Larry Page, used by the Google Internet search engine

Pagerank is algorithm that rates your website from 1 to 10. It look for Backlink count to a website and reputation of each Backlinks and according to this rule it gives Pagerank to a website.

Pagerank add rating to a index page that decide the position of index page in search results. Higher the Pagrank means Higher position in Search results.

So create good quality website with relevant content, allow Google to crawl it and index it.

Hope you like this Blog.



Monday, December 10, 2012

Website Performance Tips: Reduce HTTP requests


Whenever you request for a webpage, browser makes HTTP request for external resources like Javascript, CSS, Images and any other resource files associated with requested webpage and most of repose time spent on downloading these external resource.

Approximately 70% visitors to your website visit only homepage ( top landing page). but as developer point of view we consider homepage as any other page of website. and we include all javascript and css file which are require for whole website. therefor it increases the HTTP request and hence the page load time.


How to reduce HTTP requests?

  • Include only those external resources which are require for requested webpage. i.e. for homepage, include resource which are required for homepage only.
  • Combine different CSS file content into one CSS file.
  • Combine different Javascript file contents into one Javascript file. 
  • Use image sprite instead of different images. it will reduce the most of HTTP request.
  • Use Cache-Header to reduce HTTP request.

For better performance try to find the external resources which are not required for requested page and remove those resource from the webpage.

Use Cache-Header settings to reduce HTTP request. browser cache the external resource and won't make request for cached files for next request.

If you have number of inline images in the HTML then try to show only single default image in each inline image and load remaining images in page load event. it will improved user response time.
You can use lazyload jQuery plugin to achieve this goal.

Remember every time, site with few HTTP request will perform well and increase user satisfactions. 

Wednesday, September 12, 2012

Symfony2 Routing Mechanism

Symfony2 has basic rule for each user request as

take user request -> process request -> generate response .

Routing system act as middle man for request and  response. it accept the request then identify the controller  then identify the appropriate action method.

for example user request for http:{host_name}/demo/test

inside routing system you can build the pattern to match the request as

demo_test:
    pattern:  /demo/test
    defaults: { _controller: DemoBundle:Demo:test } 

here routing identify the bundle as "DemoBundle" then move to "Demo" controller and execute "test" action.

you can also add parameter to to routing system as


demo_test:
    pattern:  /demo/test/{page}
    defaults: { _controller: DemoBundle:Demo:test } 


here page value become parameter inside the action like

public function testAction($page

here page parameter is required parameter, it must be present in the request URI otherwise routing system looks for other routing path.
to make parameter optional add this line in routing system


demo_test:
    pattern:  /demo/test/{page}
    defaults: { _controller: DemoBundle:Demo:test , page : 1}

if page parameter is not available then default page value as 1 is added to $page  parameter.

you can pass parameter to action without defining it in routing system. each request parameter in present in request object which can be access inside the action.

Note: routing system is used to design user friendly URI. it is not necessary to add parameter to each routing system. each parameters are available to action by request object.