Skip to content

Instantly share code, notes, and snippets.

@c7x43t
Last active June 15, 2018 11:28
Show Gist options
  • Save c7x43t/798cc198a836ad1dafe4e97be54c9e39 to your computer and use it in GitHub Desktop.
Save c7x43t/798cc198a836ad1dafe4e97be54c9e39 to your computer and use it in GitHub Desktop.
// Benchmarking function (array of functions, iterations, array of arguments)
function benchmark(func,n,args){
// analyze arguments passed: empty or num/string or array
if (typeof args ==='undefined'){
args=[];
}else if(typeof args[0]==='undefined' || typeof args==='string'){
args=[args];
}
// analyze functions
if (typeof func==="function") {func=[func]}
let timer, flag=1;
// determine available timing functions - nodeJS or browser
if (typeof process!=='undefined') {timer=process.hrtime}
else if (typeof performance!=='undefined'){
flag=0;
timer = function(){return performance.now()}
}
// for all functions
let i,j,t1,t2,time,funcTmp,anonCount=1,result={};
for (i=0;i<func.length;i++){
funcTmp=func[i];
// run benchmark
t1=timer();
for (j=0;j<n;j++){
funcTmp.apply(this, args);
}
t2=timer();
time=flag?(t2[0]-t1[0])*1e3+(t2[1]-t1[1])/1e6:t2-t1;
result[func[i].name||"anonymous"+anonCount++]=time;
}
// return time in ms
return result;
}
// Benchmarking function (array of functions, array of arguments) // returns iterations/s
function benchmark(func,args){
const testTime=500; //ms
// analyze arguments passed: empty or num/string or array
if (typeof args ==='undefined'){
args=[];
}else if(typeof args[0]==='undefined' || typeof args==='string'){
args=[args];
}
// analyze functions
if (typeof func==="function") {func=[func]}
let timer, flag=1;
// determine available timing functions - nodeJS or browser
if (typeof process!=="undefined") {timer=process.hrtime}
else if (typeof performance!=='undefined'){
flag=0;
timer = function(){return performance.now()}
}
// for all functions
let i,j,n,t1,t2,time,total,funcTmp,anonCount=1,result={};
function bench(j,n){
funcTmp=func[i];
// run benchmark
t1=timer();
for (j=0;j<n;j++){
funcTmp.apply(this, args);
}
t2=timer();
time=flag?(t2[0]-t1[0])*1e3+(t2[1]-t1[1])/1e6:t2-t1;
return time;
}
for (i=0;i<func.length;i++){
n=1;
do{
time=bench(i,n);
n*=10;
}while(time<testTime/50);
iterations=Math.min(Math.ceil(testTime/time/10*n),1e7);
result[func[i].name||"anonymous"+anonCount++]=Math.floor(iterations/(bench(i,iterations)/1e3));
}
// return time in ms
return result;
}
// Benchmarking function (array of functions, array of arguments) // returns iterations/s
// Takes constant time ~1.1s/function to benchmark
function benchmark(func, args) {
for (var i = 0; i < 10; i++) _benchmark(func, args, true);
return _benchmark(func, args, false);
}
function _benchmark(func, args, warmup) {
var testTime = warmup ? 1 : 8; //ms
// analyze arguments passed: empty or num/string or array
if (typeof args === 'undefined') {
args = [];
} else if (typeof args[0] === 'undefined' || typeof args === 'string') {
args = [args];
}
// analyze functions
if (typeof func === "function") {
func = [func];
}
var timer, flag = 1;
// determine available timing functions - nodeJS or browser
if (typeof process !== "undefined") {
timer = process.hrtime;
} else if (typeof performance !== 'undefined') {
flag = 0;
timer = function() {
return performance.now();
};
}
// for all functions
var i, n, t1, t2, time, lastTime, iterations, funcTmp, anonCount = 1,
result = {};
function bench(j, n) {
var t;
funcTmp = func[i];
// run benchmark
t1 = timer();
for (j = 0; j < n; j++) {
funcTmp.apply(this, args);
}
t2 = timer();
t = flag ? (t2[0] - t1[0]) * 1e3 + (t2[1] - t1[1]) / 1e6 : t2 - t1;
return t;
}
for (i = 0; i < func.length; i++) {
n = 1;
do {
lastTime = time;
time = bench(i, n);
n *= 10;
} while (time < testTime || n < 1e7);
iterations = Math.ceil(testTime / lastTime * n / 1e2);
var samples = [];
for (var sample = 0, length = warmup ? 0.4e2 : 1.2e2; sample < length; sample++) {
if (sample > 0.2e2) {
samples.push(Math.floor(iterations / (bench(i, iterations) / 1e3)));
}
}
result[func[i].name || "anonymous" + anonCount++] = warmup ? 0 : samples.reduce(function(acc, e) {
return acc + e;
}, 0) / samples.length;
}
// return time in ms
return result;
}
// benchmark
function benchmark(func, args, live){
var limit=16, //ms
runs=200,
warmups=25,
time, iterations=0, timings;
args=Array.from(args);
//sideeffect: measure iterations
function prepare(func){
iterations=0;
time=performance.now();
while(performance.now()-time<limit){
func.apply(this,args);
iterations++;
}
}
function warmup(func,iterations){
for(var i=0;i<iterations;i++){
iteration(func);
}
}
function iteration(func){
time=performance.now();
for(var j=0;j<iterations;j++){
func.apply(this,args);
}
time=performance.now()-time;
return time;
}
// return iterations/s
function measure(func){
timings=[];
//console.log({is:measure,iterations:iterations})
for(var i=0;i<runs;i++){
timings.push(iteration(func));
}
return Math.floor(1e3/(timings.reduce((acc,e)=>acc+e,0)/runs)*iterations);
}
if(live){
prepare(func);
warmup(func,warmups);
prepare(func);
return measure(func);
}else{
var result=[];
for(var i=0;i<20;i++){
result.push(benchmark(func, args, true));
}
return result.reduce((acc,e)=>acc+e,0)/20;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment