Sunday, May 5, 2013

JSMag and GroovyMag – May 2013 Issues are here!

May 2013

From JSMag.com

What's new in jQuery 2.0 - Chetan Khurana

A major version release of the most popular JavaScript library is here! Learn more about what is new and what has changed.

Go to http://www.jsmag.com, and use the coupone code – 2me7csp

May 2013

From GroovyMag.com…
Grails Plugins "Inline" - Chetan Khurana

Customizing a grails plugin which is commonly referred to in more than one application can cause many issues. Declaring plugins as inline solves these issues!

Go to https://www.groovymag.com/, and use the coupon code – 21yoc8k.

Steps to follow for getting the issues -

1. Register at the URL – JSMag  / GroovyMag

2. Go to the CART option and use the coupon codes given above;

3. Go to your account option to download and access your free copy of MAY 2013 issues!

Feel free to blog, twitter, give to colleagues or otherwise disperse.

Friday, May 3, 2013

JavaScript & DOM Reflows

js45_400

This article is from the November 2012 issue of the JSMag publication.

In here, we discuss the concept of DOM reflows and why they are expensive (and should be avoided).

I am replicating the article in its entirety as below and not to mention with full input & collaboration-credit to my good friend Chandan Luthra!

Hope it is helpful to my friends out there!


If you use JavaScript for your web development, you need to know about DOM reflows. Here we discuss what reflows are and why they happen. We also go through ways to minimize reflow issues in order to improve java-script performance.

 

What is a reflow? – an overview

Let us start evaluating a simple HTML or XHTML webpage. This webpage is bound to include references to resources such as CSS, external java scripts (JS) and images etc. and all these collectively contribute to the final look and feel of the webpage UI.

These relative DOM elements are connected in a hierarchical/structural layout, and are encompassed in a logical pattern called a frame. This frame may be a child or a parent of subsequent inner frame element(s). Each of these rectangular frames has specific attributes like width / height and so on.

During the rendering process of the webpage, the entire DOM element model is traversed by the browser. This traversal starts from the absolute parent (i.e. the <html> element) then proceeds or flows through the entire DOM. A reflow refers to the revalidation of page elements for (re)rendering the entire DOM after any DOM updates.

 

Wait a minute! How is this different from a repaint?

Both DOM reflow and repaint events can happen on an HTML page and reveal any element’s altered state. A repaint occurs when changes are made purely at a superficial or visible level, like changing the color attributes of an element, and which do not affect element layout at all. Examples of this can include modifying table borders, div related visibility or page /element level background color.

A repaint too incurs some expense as the browser engine has to re-process the visibility criteria for the whole DOM tree.

 

When does a reflow happen?

A reflow is more or less quite a substantial change and is triggered whenever a user or script action makes changes to the DOM tree.

This may be due to changing a style (thereby changing layouts), changing any element’s className property or simply changing browser level properties, like the browser’s window size etc.

As soon as such a change is detected by the browser engine, the actual changed element and its subsequent child elements have to undergo a reflow so as to cascade their parent’s update.

This change is cascaded further down the line and all the dependent elements appearing after the updated DOM element have to be reflowed in order to adjust to the newly calculated layout.

Moreover, the parent or preceding elements will also need a reflow to acknowledge the layout changes of their child elements. And after this reflow is implemented all over, a final repaint request is issued for the DOM.

Reflows can be classified as instant (as in case of a response to user / script processing) or they may be required as a part of page load process, when page elements are loaded in a sequence (for example in stream based rendering).

 

So why are reflows actually bad?

Sometimes reflows are actually required. An example is using script based animation sequences which is handled via reflows. But an “un-intentional reflow” due to coding issues in scripts and poorly planned style classes is what leads to expensive and performance hampering behavior.

Reflows are bad since they are very expensive and counterintuitive to performance. They can and do lead to slow JavaScript and a poor user experience. The scenario gets even worse for devices with low processing power such as phones and tablets or any other device where processing power is at a premium. A reflow is more or less akin to refreshing or performing a fresh layout for the entire page, which is surely heavy and undesired.

 

Some common reflow events and how to minimize them

Let us go through some easily avoidable reflow instances which are mainly caused due to coding oversight.

 

Updating DOM elements in a loop

Assume a scenario where-in we need to modify an element in the DOM tree. For example, suppose we have a DIV parent having some child anchor tags and we need to change the className attribute for these anchor tags within this DIV element.

function updateDivAnchors(divElement, newAnchorClass){
var anchorObjs = divElement.getElementsByTagName('a');
var totalAnchorObjs = anchorObjs.length;
for (var i = 0; i < totalAnchorObjs; i ++) {
anchorObjs[i].className = newAnchorClass;
}
}

Listing 1: User defined method that replaces/adds a className to the div elements


At a glance, the function in Listing 1 will surely do what is intended, but we have a major reflow issue here. This code fires a DOM reflow for every update to each anchor element (as we have a loop in effect).

Now, if this DIV has 10 child anchor elements, the entire DOM tree would undergo an un-necessary refresh 10 times. This is a lot of undesired reflow activity!

Any better solution should avoid touching the DOM during the DIV update. One way this can be achieved is by using a modified script that would –


1) Remove the DIV (to be updated) from the DOM;

2) Update the anchor elements in that DIV;

3) Re-add the DIV back at its location in the DOM.


This approach can be further refined if we make use of the DocumentFragment object.

As per this definition at Mozilla Developer,


A DocumentFragment is a minimal document object that has no parent. It is used as a light-weight version of document to store well-formed or potentially non-well-formed fragments of XML.


Thus, a DocumentFragment can be created on the fly to create a temporary DIV node as shown in Listing 2.


1 var docFragment = document.createDocumentFragment();
Listing 2: Example to create DocumentFragment object on the fly

After this, we can iterate and populate this fragment, manipulate the anchor elements, and add back (rather replace) the actual DIV with this updated fragment. So basically, the number of reflows which was proportional to the number of anchor tags in the previous example is now limited to just two reflows! This is bound to speed up the action for that page.

 

Manipulating visibility attribute to update element properties


Another way to reduce reflows is to use hidden elements. In case an element or its children require many separate changes (which cannot be combined into a single repaint), the element’s visibility style can be set as display:none, the changes made, and then the element can be set back to its normal display.

Changing properties or attributes of hidden elements does not trigger the repaint request since it is not being displayed.

Similar to the earlier mentioned fix, in this case too we need only two reflows. One when the element is hidden, and second when it is made to appear again, but the overall effect can be much faster (issues like abrupt UI refresh for dependent elements can be avoided by using a preset offset value)

 


Changing an element’s style attributes – style vs. class update


If we need to make several style related updates for an element, the same should be done at one go to minimize the number of reflows.

We generally code to set element styles one at a time, as shown in Listing 3.


1 var targetElement = document.getElementById('anyDivId');
2 targetElement.style.background = '#333';
3 targetElement.style.color = '#fff';
4 targetElement.style.border = '1px solid #00f';
5
Listing 3: Code to change/update few cascade styles of an HTML element

Here, for every single style update a new reflow request is invoked which is again an overhead for the DOM. A better and faster approach would be to create a CSS class for the identified style changes and update the same as shown in Listing 4.


1 document.getElementById(' anyDivId ').className = 'newDivClass';
Listing 4: Example of applying CSS class from JavaScript

Note: There are many more ways which can be utilized for further minimizing the nuisance due to reflows. Please refer the links at the end of the article for the same.

 


Figures to back the impact of reflows


The point of the entire discussion is that it is actually JavaScript which triggers the reflows and repaints. Avoiding the reflows will make the page appear faster not because of a speedier script engine but because the browser is doing less of reflow and repaint activity.

The reflow minimization above may or may not lead to any tangible/actual performance benefits (like JavaScript speed up etc.). But the advantage will be that the browser will now show a faster response time against actual user actions. We will enable the browser in such a manner so that it spends lesser time requesting expensive I/O and processor resources for redundant DOM reflows.

If we use any pure JavaScript profiling tool, the reflow related code optimization will show little or no affect. We are not really bothered about speeding up the JS code; rather, we need to measure the effect it has on the browser rendering for that modified page.

As a reference, the link below shows the reflow in action (Wikipedia’s home page reflow video) –

http://video.google.com/videoplay?docid=-5863446593724321515&hl=en

If you want to track the paint events for your webpage, you need Firebug (https://addons.mozilla.org/en-us/firefox/addon/firebug/) and this excellent add-on https://addons.mozilla.org/en-US/firefox/addon/firebug-paint-events/.

This will show the DOM reflow/paint events in the Firebug console.

 


Conclusion & follow up


The above primer will surely be helpful for you in minimizing un-necessary reflow events. Below are some links for pursuing the reflow discussion further:

· A helpful note on HTML reflowhttp://www-archive.mozilla.org/newlayout/doc/reflow.html

· When does JavaScript trigger reflows and rendering?http://mir.aculo.us/2010/08/17/when-does-javascript-trigger-reflows-and-rendering/

· Page Rendering & Reflow Performancehttp://blog.monitor.us/2012/04/page-rendering-reflow-performance/

· CSS solutions for Browser Reflows & Repaints & how do they affect performancehttp://ajaxian.com/archives/browser-reflows-how-do-they-affect-performance




Tuesday, April 9, 2013

JSMAG April 2013!

April 2013

So here is the latest edition of JSMag! And you can use the coupon code zx4dr03 to get your copy for free!

Please feel free to blog, twitter, give to colleagues or otherwise disperse.

Find my article about “Modernizr - Detecting HTML5 and CSS3 Features” in this month;s issue!

Go here for the full issue - http://www.jsmag.com/main.issues.description/id=67/

Follow the steps in my earlier post - http://singularitycometh.blogspot.in/2013/02/get-both-jsmag-groovymag-for-feb-2013.html on how to get the issues. But please use the code in this post!

Thanks!

Monday, March 25, 2013

Agile programming – Now in family mode !

Excellent advice, and all in all a great TED talk.

You can refer this Life hacker article which talks about applying the "agile" philosophy to family life.

Also refer the direct YouTube link for the TED talk - Bruce Feiler: Agile programming -- for your family

Wednesday, March 13, 2013

Cloud based IDEs – Now code anywhere!

image

The backbone of any efficient Development Cycle is a versatile and intuitive IDE.

An IDE (or an Integrated Development Environment) provides comprehensive facilities for coding in a software development cycle.

Traditional IDEs are bound to the physical work environment and locally to the development system.

Enter the Cloud based IDEs

Cloud based IDEs are getting popular by the day. All they require is a web browser, where-in the editor can be used to write code which can also be stored in a cloud based repository.

The cloud is slowly getting the popularity and versatility it promised as a concept. It is already facilitating stuff like online backup creation, document sharing & collaboration (via cloud based CMSs) and data-sharing for distributed web channels like social networking.

The main beneficiary of a Cloud based IDE is the Mobile application development front. It truly integrated application development on a distributed level, and different aspects of a mobile application (like native vs. web) can be seamlessly developed, tested and integrated. 

For the cloud based IDEs, you are having many choices.

Some of them are -

and so on.

But it is the latest entry which excites me the most!

image

Yes, it is the Eclipse IDE which has recently launched the Orion IDE. Orion brings the experience and developer ease-of-use (and confidence) of Eclipse to the Cloud IDE arena.

Orion is still in progress, however, and it is limited to client based / front-end web development (i.e. HTML and JavaScript).

It features out-of-box Firebug integration and the usual Eclipse UI with code completions for starters!

One thing is for sure, the coming days are going to be interesting for the development arena.

Now all we need is an in-fallible, always on, (cheap!) broadband connection. Sigh!

Tuesday, February 26, 2013

The Firefox O.S. – Mozilla’s latest thunder!

The Firefox OS, codenamed Boot to Gecko (aka B2G) has been launched quite recently and is a topic of speculation in tech-circles. This O.S. is uses a Linux kernel and runs HTML5 based apps (which couple up with the Firefox browser)

Firefox O.S. aims at controlling the hardware (integration) using JavaScript (now how cool is that!), and its basic reason for coming to existence is ‘to create a true OSS (open source) O.S. for which apps would freely be available on the Firefox Marketplace’.

The first Firefox O.S. based smartphones should be shipping as early as the the first half of 2013.

From the GSMArena -

Mozilla is officially launching their Firefox OS for mobile phones and are proud to announce that they have deals with 18 carriers, a partnership with Qualcomm to use their Snapdragon chipsets and four manufacturers - ZTE, Alcatel, LG and Huawei. The first two also showcased phones with Firefox OS at the MWC.

Also, as per the latest reports, Sony is planning to launch its own Firefox O.S. based device in early 2014.

This could be a game changer for next year’s mobile technology (imagine the app development scenarios!)

Only thing which remains to be seen is how fast can market penetration actually help Mozilla, and oh, there is the current popularity of iOS and Android to add spice to the ‘market dominance race’ too!

Wednesday, February 13, 2013

Samsung Galaxy S4 Final Specs! Everything official about it.

Just the links here for the while -

Korean site - http://www.ddaily.co.kr/news/news_view.php?uid=101028

Translation URL – HERE

One confirmation is that Home button is there.

Will post when further details become available.

Sunday, February 10, 2013

Get both JSMag & GroovyMag for Feb 2013, free!

That and the latest issues of JSMag and GroovyMag!

Get the latest JSMag issue where yours truly has a “good enough to brag” article on HTML5 and a write-up in the and GroovyMag’s Plugin corner too (both with my good Friend one who cannot be named*)

February 2013

This is how you get it -

1. Register at the URL - http://www.jsmag.com/ for JSMag

2. Go to the CART option and use the following coupon codes –> rcmm9pb

3. Go to your account option to download and access your free issue of JSMag Feb 2013!

February 2013

This is how you get it -

1. Register at the URL - http://www.groovymag.com/ for GroovyMag

2. Go to the CART option and use the following coupon code – 7p0d237

3. Go to ‘My Account’ option and select the ‘Review magazine purchases’ to download and access your free issue of GroovyMag Feb 2013!

Simple!

As of writing, this has only 20 uses left! So hurry up and get this while it lasts!

You can thank me in the comments ;-)