getElementById()を計ってみる その2

同じコードをFirefoxで計ってみたら、それほど差がなかった。

プロファイラはすこし改造。


Profiler = new Object();

Profiler.watches = new Object();

Profiler.start = function(name) {
var watch = Profiler.watches[name];

if (!watch) {
watch = new Array();
watch.sum = function() {
var sum = 0;
for(var i = 0; i < watch.length; i++) { sum += watch[i]; }
return sum;
}
Profiler.watches[name] = watch;
}

var start = new Date();

return function() {
var stop = new Date();
var duration = stop - start;
watch.push(duration);
};
}

Profiler.profile = function(name, transaction) {
var stop = Profiler.start(name);
transaction();
stop();
}

Profiler.report = function() {
var name = null;
var total = 0;
var watch_sum = null;
var report = new Array();

for(name in Profiler.watches) {
watch = Profiler.watches[name];
total += watch.sum();
}

report.push('total: ' + total + ' [ms]');

for(name in Profiler.watches) {
watch = Profiler.watches[name];
watch_sum = watch.sum();
report.push('---');
report.push('name: ' + name);
report.push('%: ' + (watch_sum / total * 100));
report.push('count: ' + watch.length);
report.push('ms: ' + watch_sum);
report.push('ms/call: ' + (watch_sum / watch.length));
}

alert(report.join('\n'));
}

Profiler.clean = function() {
Profiler.watches = new Object();
}