Skip to content

Instantly share code, notes, and snippets.

@josketres josketres/Benchmark.js
Last active Aug 26, 2015

Embed
What would you like to do?
Small library to do benchmark in javascript
var Benchmark = function() {
};
Benchmark.prototype.start = function() {
this.start = new Date().getTime();
console.time('Benchmark');
};
Benchmark.prototype.stop = function() {
this.stop = new Date().getTime();
console.timeEnd('Benchmark');
};
Benchmark.prototype.read = function() {
this.data = localStorage.getItem('benchy');
if(this.data) {
this.data = JSON.parse(this.data);
} else {
this.data = [];
}
};
Benchmark.prototype.write = function() {
this.read();
this.data.push(this.stop - this.start);
localStorage.setItem('benchy', JSON.stringify(this.data));
};
Benchmark.prototype.stats = function() {
this.read();
console.log(typeof this.data);
var avg = this.data.reduce(function(prev, curr, index, arr) {
if(index === arr.length-1) {
return (prev + curr) / arr.length;
}
return prev + curr;
}, 0);
console.log('avg: ' + avg + 'ms');
console.log(this.data);
};
Benchmark.prototype.reset = function() {
localStorage.setItem('benchy', '[]');
};
Benchmark.prototype.repeat = function(times) {
this.read();
if(this.data.length < times) {
window.location.reload();
} else {
this.reset();
}
};
window.benchy = new Benchmark();
var Benchmark=function(){};Benchmark.prototype.start=function(){this.start=(new Date).getTime(),console.time("Benchmark")},Benchmark.prototype.stop=function(){this.stop=(new Date).getTime(),console.timeEnd("Benchmark")},Benchmark.prototype.read=function(){this.data=localStorage.getItem("benchy"),this.data=this.data?JSON.parse(this.data):[]},Benchmark.prototype.write=function(){this.read(),this.data.push(this.stop-this.start),localStorage.setItem("benchy",JSON.stringify(this.data))},Benchmark.prototype.stats=function(){this.read(),console.log(typeof this.data);var t=this.data.reduce(function(t,e,a,o){return a===o.length-1?(t+e)/o.length:t+e},0);console.log("avg: "+t+"ms"),console.log(this.data)},Benchmark.prototype.reset=function(){localStorage.setItem("benchy","[]")},Benchmark.prototype.repeat=function(t){this.read(),this.data.length<t?window.location.reload():this.reset()},window.benchy=new Benchmark;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.