This is the first in a series of articles I plan to write on improving performance in JavaScript heavy web-applications. More specifically I will discuss some of the ways you can improve on JavaScript based animations, reducing object chains, cross-browser DOM, clever ways of writing/designing objects and so on.
Improving on Animations
No matter what type of animation it is if it’s written using JavaScript it’s most likely going to make use of setTimeout or setInterval. Both of these function work like a timeline in Flash and therefore it’s ideal for doing animations.
To begin with setInterval is much faster than setTimeout. The reason is setInterval causes a function to continously run until it’s stopped using clearInterval, where as setTimeout runs a function after a given time and stops; therefore if you wanted to animate something using setTimeout you would somehow have to keep calling setTimeout (until the animation ended) where as in setInterval this is done only once.
The down-side of this is setInterval is supported only by Internet Explorer so to get really high performance you might want to write one version (separate files) of your JavaScript that’s geard towards Internet Explorer and another for Netscape (which supports setTimeout).
Next time I will discuss how to run multiple animations at once using one-thread (or one setTimeout or setInterval).
How I came to this is not relevant I think, so I will jump straight to the examples. The idea was to convert this XML document to produce this output. So I set out to do it — and it was finally done using this XSLT stylesheet.
The idea is quite basic. Count the number of ‘I agree’ responses and store it in a variable. Count the number of ‘I disagree’ responses and store it into another variable, and so on…
Once that’s done, the count values are multiplied with the expansion factor (variable: efactor) and this number is set to the height of a div element. That’s all.
Let’s all resolute to producing a standardized web!
The STL sorting algorithm allows you to pass in a function pointer that the algorithm will use to compute the sorting transformation. This can be particularly useful when you need a set of data sorted in an order other than plain ascending or descending. The STL sorting algorithm further gurantees the sorting to be done in O(n lg*n) time (given your function is constant time).
The function pointer however has to meet the following requirements:
Here’s a small sample of how this can be used:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string order;
bool newSortType(const string& s1, const string &s2) {
int i=0;
while(s1[i] == s2[i] && s1[i]!='