Saturday, 16 August 2008
Performance Testing in JavaScript
If you make them wait, they will leave.
So the speed of your JavaScript applications is crucial.
Fortunately, it turns out that JavaScript applications are ripe for performance optimization.
This requires a strong testing framework... enter JSTR.
String Concatenation Performance
A simple example of the type of benefit available from performance testing can come from testing String concatenation.
Javascript allows the use of "+" to concatenate Strings, but it turns out to be rather inefficient if you are building a very big string, eg: dynamically building HTML from AJAX.
Instead, the use of a Array (wrapped to provide a StringBuffer object) provides a huge boost in performance.
[cc lang="javascript" tab_size="2" lines="40"]
function StringBuffer() {
this.buffer = [];
}
StringBuffer.prototype.append = function append(string) {
this.buffer.push(string);
return this;
};
StringBuffer.prototype.toString = function toString() {
return this.buffer.join("");
};
var buf = new StringBuffer();
buf.append("this is being added");
alert(buf.toString());
[/cc]
To test this against a normal concatenation:
Copy the String below, and then paste it into the IMPORT function in JSTR.
[cc lang="javascript" tab_size="2" lines="40"] jstr_StringBuffer=KHtuYW1lOiJTdHJpbmdCdWZmZXIiLCBkZXNjOiJUaGlzIHRlc3QgY29tcGFyZXMgdGhlIHNwZWVkIG9mIGJ1aWxkaW5nIGEgbGFyZ2Ugc3RyaW5nIGJ5IGNvbmNhdGVuYXRpbmcgc3RyaW5ncyBvciB1c2luZyBhIFN0cmluZ0J1ZmZlcihhIHdyYXBwZWQgYXJyYXkpLlxuQ29tcGFyZXMgYm90aCBsb25nIGFuZCBzaG9ydCBzdHJpbmcgYnVpbGRpbmcuXG4iLCBsb29wczoiMjAwMCIsIGl0ZXJhdGU6IjUiLCB0ZXN0czpbe25hbWU6InNob3J0U1RSIGNvbmNhdCIsIHNldHVwOiJhU3RyaW5nID0gXCJhZGRpbmcgXCI7XG5iU3RyaW5nID0gXCJTdGFydCBcIjsiLCBjb2RlOiJiU3RyaW5nICs9IGFTdHJpbmc7IiwgaW5jOnRydWV9LCB7bmFtZToibG9uZ1N0ciBjb25jYXQiLCBzZXR1cDoiY1N0cmluZyA9IFwiIGFkZGVkIGFkZGVkIGFkZGVkIGFkZGV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nQnVmZmVyIiwgc2V0dXA6ImVTdHJpbmcgPSBcIiBNb3JlXCI7XG5mU3RyaW5nID0gXCJTdGFydCBcIjtcblxuU3RyaW5nQnVmZmVyID0gZnVuY3Rpb24gKCkgeyBcbiAgIHRoaXMuYnVmZmVyID0gW107IFxuIH0gXG5cbiBTdHJpbmdCdWZmZXIucHJvdG90eXBlLmFwcGVuZCA9IGZ1bmN0aW9uIGFwcGVuZChzdHJpbmcpIHsgXG4gICB0aGlzLmJ1ZmZlci5wdXNoKHN0cmluZyk7IFxuICAgcmV0dXJuIHRoaXM7IFxuIH07IFxuXG4gU3RyaW5nQnVmZmVyLnByb3RvdHlwZS50b1N0cmluZyA9IGZ1bmN0aW9uIHRvU3RyaW5nKCkgeyBcbiAgIHJldHVybiB0aGlzLmJ1ZmZlci5qb2luKFwiXCIpOyBcbiB9OyBcblxuYnVmID0gbmV3IFN0cmluZ0J1ZmZlcigpO1xuYnVmLmFwcGVuZChmU3RyaW5nKTsiLCBjb2RlOiIgYnVmLmFwcGVuZChlU3RyaW5nKTtcblxuIiwgaW5jOnRydWV9LCB7bmFtZToibG9uZ1N0ciBTdHJpbmdCdWZmZXIiLCBzZXR1cDoiXG5oU3RyaW5nID0gXCJTdGFydCBcIjtcblxuYnVmMiA9IG5ldyBTdHJpbmdCdWZmZXIoKTtcbmJ1ZjIuYXBwZW5kKGhTdHJpbmcpOyIsIGNvZGU6ImJ1Zi5hcHBlbmQoY1N0cmluZyk7XG5cbiIsIGluYzp0cnVlfV0sIHZlcnNpb246MX0p [/cc]
Just to give you an idea...
StringBuffer is just faster for short strings
and about 50 times faster for long strings...!
My thanks to Pavel Simakov for this.
Thursday, 14 August 2008
A 'foolish' new tool for Javascript
instead I have been waiting very patiently...
BUT now it is time to SHOUT!
JSTR(Jester) is out of the box!
JSTR is the first 'Browser based' (ie:Cross-browser ):
- Testing
- Benchmarking
- Optimization
tool for javascript.
With both import & export of Test-Sets, as well as auto-generated URLs, JSTR lets you easily share your code with everyone else, so they can try it for themselves.
Now, when you write about your new discovery, put a link to the test-set in your blog and let everyone else actually try it.
Find out about the new funny man for JavaScript.
Watch the video to learn more
Saturday, 19 July 2008
Timing is everything
A simple way to track timing in Javascript.
[cc lang="php" tab_size="2" lines="40"]
// TIMESTAMP Function
function timestamp(){return (new Date()).valueOf();}
// TIMER Function, wraps timing arround function call
function timer(f){
var t1 = timestamp();
for(var i = 0; i < 1000; i++){
f();
}
var t2 = timestamp();
return t2-t1;
}
// The function to test
var testFunc = function(){
var v = (window["DoYouBelieveInSpeedReading"])?true,false;
}
// Now call the timer
alert("time taken="+(timer(testFunc)/1000) + " seconds" );
[/cc]
The core is converting a date object into milliseconds: date.getValueOf(). This will give you a Long of the number of millseconds since 1970.
so var t1 = timestamp(); will store the start time.
Then do the thing you want to test... I would suggest a very large number of times... I needed to make it 100,000 for the Regex below.
and var t2 = timestamp(); becomes the end time.
Now totalTimeTaken = t2 -t1; in millseconds.
Finally time = totalTimeTaken/1000; to get the number of seconds that the function took.
I wrapped it all up in a function timer...
which can be called by
alert("time taken="+timer(myFunc)/1000);
I recently used this to test String matching functions, to find that indexOf is blisteringly fast, followed closely by pattern.test, and then way behind that String.match .
For 100,000 repetitions:
String.match: 1300ms
pattern.test: 360ms
String.indexOf: 180ms
So the moral of todays story is...
- use indexOf if you can
- pattern.test if you must have REGEX or more than one match
- and AVOID String.match if you can
Just remember that premature optimization is a bad thing...
Happy Testing...!
Friday, 25 April 2008
Better Task control on Windows (NT/XP/Vista)
e.g.: checking to see if my new application has a memory leak etc..
Now I have just discovered it has an even better toy-set built into it.
If you have never tried it, download and install ProcessExplorer.
The run it.
You will get a view like task manager, but with much better granularity in the processes.
Next comes the standard view:
select View > System Information
Now, for the "Piece de Resistance"...
Goto Options > Replace Task Manager.
Now try CTRL-ALT-DELETE...
Windows Task Manager has been replaced by the more powerful ProcessExplorer.
Saturday, 29 March 2008
How to make a difficult descision - an analysis of Global Warming
I recently came across the series of videos produced by wonderingmind42 on the risk management analysis of the current gloabal warming crisis. Also available on manpollo.
He proposes a very compelling argument. And what is more, it is simple.
Also very interesting, is that his very detailed videos (there is nearly 6 hours in total) also teach the principals he is using to perform the analysis... and they are entertaining...! WOW.
He is a science teacher, but still I am impressed by the amount that can be learnt, from watching even a small fraction of his available videos.
I have long awaited someone bringing back interest and fun into important educational topics, this is a very nice example.
If you have any interest in Global Warming, or you would like to learn more about scientific principals, or you would like to undersand more about risk analysis, then I seriously suggest you have a look.
Here is the first video, covering the basics:
[video]http://www.youtube.com/watch?v=mF_anaVcCXg[/video]
Then more on how science works:
[video]http://www.youtube.com/watch?v=6A58X73GnzE[/video]
And finally some on risk analysis
[video]http://www.youtube.com/watch?v=LwuDDZ5HM_U[/video]
If you like any of this, take some time and watch more of his videos.
You will learn a lot.


