Last active
August 26, 2015 09:31
-
-
Save josketres/2f832f95d7de7976d1dc to your computer and use it in GitHub Desktop.
Small library to do benchmark in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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