Benchmarking Javascript
There are a series of ways by which you could benchmark Javascript code. Either by using the Firebug console API, example:
console.time("first");
// some javascript code
console.timeEnd("first");
Another way would be the one which ppk suggested on his blog.
function testIt() {
var startTime = new Date().getTime();
// actual DOM functionality to be tested goes here
setTimeout(function () {
var endTime = new Date().getTime();
var result = (endTime-startTime)/1000;
// print result
},10)
}
The reason why the result is printed through a different function set to run on timeout is:
(…) some browsers only applies the result of the test (i.e. the changes in the DOM you want to test) to the screen after the function has ended entirely. (…) The correct way of conducting this test is setting a timeout for reading out the end time. The function ends when the in-memory DOM manipulation has been done, which allows the browser to apply the changes.
If that’s the case, we could also use the following function for benchmarking Javascript code (a more flexible version):
function benchmark(func) {
var startTime = new Date().getTime();
func();
var endTime = new Date().getTime();
return (endTime-startTime)/1000;
}
// as for usage
time = benchmark(function() {
// javascript code to benchmark
});
It is completely unrelated to any aspect of security, but there are some topics that just make me blog about… Anyway, very soon I’ll post on, have a few projects I’m working on lately.

