I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, 29 April 2010

Javascript Performance Talk

I watched the javascript performance talk from Nicholas C. Zakas at Google Tech Talks: Nicholas C. Zakas: Speed Up Your JavaScript.

  • Scope Management
  • Loop optimization
  • About HTMLCollection Live objects
  • Document Fragments - postponing reflow

Scope Management

The tip given to improve efficiency is to use local variables, because variables which are in the local scope are accessed first. Every time a global or a variable from a prototype or a dot object variable is accessed twice. Use a local variable.

Another issue related to the scope aspect is that using functions and closures comes with a certain cost.

Loops Optimization

Loops can be optimized in different ways, when avoiding unnecessary work.

About HTMLCollection Live objects

The presentation explains that the collection returned by methods such as: .getElementsByTagName, ... return HTMLCollection objects which remain 'live', that is they get updated. A typical example is the following loop:

var divs = document.getElementsByTagName('div');

for (var i = 0; i< divs.length; i++){
var div = document.createElement('div');
document.body.append(div);
}
The collection in divs is an HTMLCollection. The body of the loops goes over the loops and adds a div for every object in the collection. But since new objects are added to the loop every time one goes through the loops, the loops never ends !!!!!

DocumentFragment - postponing reflow

Reflow occurs when the the size of the elements on the page are recalculated. To improve performance it is useful to minimize the number of reflows.

In general, Nicholas C. Zakas explained that reflow happens at five particular moments in browsers:

  • Page load
  • browser window resize
  • adding and removing elements
  • setting style attribute of elements
  • (sometimes) accessing the style elements of the nodes

To prevent useless reflows, one can use a document fragment in order to create a quite complete element as a document fragment and then add it to the DOM. In this way, reflow is only performed when the element is added to the page.

To minimize the access to the style attributes of elements, a better way to perform that is to change the class of the element, i.e: use .className instead of .style.

Friday, 23 April 2010

Dojo Toolkit - Basics

This is a high level overview of the dojo toolkit.

  • Function called on load
  • package framework
  • a DOM element retrieval mechanism
  • a number of Widgets
  • Helper Methods
  • Dojo Array Methods
  • dojo.connect - Event mechanism
  • Parsing, formatting and validation of numbers and dates
  • Ajax request tools
  • An history and bookmarking mechanism
  • Event System tools
  • Animation tools
  • I18n tools (Internationalization)

Loading the dojo code

The code of dojo can be loaded from http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js. I would like to try loading it from the web server.

Function called on load

As for other web frameworks, you can have different function called on load:

dojo.addOnLoad(funtion( // code to perform on load ));

package framework

The dojo toolkit provides a package mechanism, the code is used in the following way:

dojo.require('dojo.fx');
loads the dojo.fx package.

a DOM element retrieval mechanism

Dojo provides a number of methods to retrieve elements of the DOM document. For instance, dojo.byId('myId'); retrieves the element with the id 'myId' just as document.getElementById would.

Just as other frameworks dojo provides also a query mechanism.

From the dojo documentation:

// all <h3> elements
dojo.query('h3')
// all <h3> elements which are first-child of their parent node
dojo.query('h3:first-child')
// a node with id="main"
dojo.query('#main')
// all <h3> elements within a node with id="main"
dojo.query('#main h3')
// a <div> with an id="main"
dojo.query('div#main')
// all <h3> elements within a div with id="main"
dojo.query('div#main h3')
// all <h3> elements that are immediate children of a <div>, within node with id="main"
dojo.query('#main div > h3')
// all nodes with class="foo"
dojo.query('.foo')
// all nodes with classes "foo" and "bar"
dojo.query('.foo.bar')
// all <h3> elements that are immediate children of a node with id="main"
dojo.query('#main > h3')

A number of Widgets

The dojo toolkit provides a number of widgets. These can be retrieved by the dijit.byId() method.

Dojo Array Methods

Dojo provides some other useful methods for example: dojo.forEach(). A similar method can be used on the resuklt of a dojo.query().

Dojo also provides filter and map functions. filters an array and returns all the elements verifying a given property. The map function applies some function on all the elements of an array and returns a new array of new values.

The functions dojo.some() and dojo.every() checking that some property is true for some, respectively all the elements of the array

dojo.connect - Event mechanism

The dojo toolkit provides the dojo.connect methods which allows to connect some event to a given event.

Parsing, formatting and validation of numbers and dates

Ajax request tools

An history and bookmarking mechanism

Event System tools

Animation tools

I18n tools (Internationalization)

Wednesday, 21 April 2010

Use JQuery in firebug with jQueryfy

Here is a great presentation on how to use firebug to test interactively jquery.

JQueryfy is a bookmarklet to load jquery in any web page easily.

So all you have to do is:

  1. go to some web page.
  2. Load Jquery with jQueryfy
  3. then start firebug

Then go to the console of firebug, and type jQuery(). This should return
"function()".

In order to have all the images disappear for example, you can use:
jQuery('img').hide()

Of course you need to use a correct selector for your queries. But isn't that just great ?

Note: I had to update my version of firebug to have all this working.

Sunday, 17 January 2010

Dojo Toolkit - Yet another javascript toolkit

After looking at roo, I noticed that they used the Dojo Toolkit for their default implementation of the web toolkit of the page. I Have not yet done a real comparison of the two frameworks. It seems to have more or less the same functionality as ext-js. I will have to keep an eye on both systems. At least from the licence point of view is Dojo more appropriate for companies, since it uses a BSD/Apache licence, where as ext-js under GPL ( I do not think that it has also a LGPL version apart from the core). Dojo seems to be composed of three parts:
  • Dojo core
  • dijit
  • dojox
Dojo core contains a number of utility methods for querying web pages content and perform operations on it. Dijit is a widget mechanism. Dojox are extensions based on dijit.