Small helper to do some time measuring. timerEnd returns the time in ms, also the timers object contains information about how many times the timer with this name was used, the sum of all measured times and the average of the measurements. I find this quite useful, since the measured time for an operation depends on many factors, so it's best to measure it several times and look at the average.
var timers = {};
function timer(name) {
timers[name + '_start'] = window.performance.now();
}
function timerEnd(name) {
if (!timers[name + '_start']) return undefined;
var time = window.performance.now() - timers[name + '_start'];
var amount = timers[name + '_amount'] = timers[name + '_amount'] ? timers[name + '_amount'] + 1 : 1;
var sum = timers[name + '_sum'] = timers[name + '_sum'] ? timers[name + '_sum'] + time : time;
timers[name + '_avg'] = sum / amount;
delete timers[name + '_start'];
return time;
}
default