Skip to content

Instantly share code, notes, and snippets.

@marmotz
Last active December 27, 2015 11:09
Show Gist options
  • Save marmotz/7316220 to your computer and use it in GitHub Desktop.
Save marmotz/7316220 to your computer and use it in GitHub Desktop.
VERY simple benchmarker for code. Produce a light report on console.
/**
* VERY simple benchmarker for code.
* Produce a light report on console.
*
* USAGE:
*
* Bencher.get('a bench description').start();
*
* // code here
*
* Bencher.get('a bench description').log('first code done');
*
* // code here
*
* Bencher.get('a bench description').log('second code done');
*
* Bencher.get('a bench description').stop();
*/
var Bencher = {
benchs: {},
get: function(name) {
if (this.benchs[name] === undefined) {
this.benchs[name] = {
bencher : this,
name : name,
markers : [],
log: function(description) {
this.markers.push(
{
time : new Date().getTime(),
description : description
}
);
},
start: function() {
this.log('Start');
},
stop: function() {
this.log('Stop');
var startTime = 0;
var previousTime = 0;
console.group('Bench: ' + this.name);
for (var i = 0; i < this.markers.length; i++) {
var marker = this.markers[i];
if (startTime === 0) {
previousTime = startTime = marker.time;
}
console.log(
'%s : %dms (+%dms)',
marker.description,
marker.time - startTime,
marker.time - previousTime
);
previousTime = marker.time;
}
console.groupEnd();
delete this.bencher.benchs[name];
}
};
}
return this.benchs[name];
}
};
Bench: get team schedules bencher.js:30
Start : 0ms (+0ms) bencher.js:39
begin ScheduleRepository.findTeamSchedules : 1ms (+1ms) bencher.js:39
raw schedules loaded : 968ms (+967ms) bencher.js:39
schedules sorted by week and TeamMember : 975ms (+7ms) bencher.js:39
end ScheduleRepository.findTeamSchedules : 996ms (+21ms) bencher.js:39
data loaded : 996ms (+0ms) bencher.js:39
Stop : 996ms (+0ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment