Skip to content

Instantly share code, notes, and snippets.

@mdamien
Created July 8, 2015 16:04
Show Gist options
  • Save mdamien/43b193ea16a32e3f62ba to your computer and use it in GitHub Desktop.
Save mdamien/43b193ea16a32e3f62ba to your computer and use it in GitHub Desktop.
function logtitle(x) {
document.title = x
console.log(x)
}
function milli2nice(m) {
var r = Math.round(m*1000)/1000;
return r+"ms"
}
function median(values) {
values.sort( function(a,b) {return a - b;} );
var half = Math.floor(values.length/2);
if(values.length % 2){
return values[half];
}
return (values[half-1] + values[half]) / 2.0;
}
function tocsv(results, name){
var csvContent = "data:text/csv;charset=utf-8,";
csvContent += results.join('\n');
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", name);
link.click();
}
function bench(name, fn, options){
options = options || {};
samples = options.samples || 80;
maxtime = options.maxtime || 3; //seconds
var start, total, times = [];
var i = 0;
for(;i < samples; i++){
start = performance.now();
fn()
total = performance.now()-start;
//console.log('cycle',i,milli2nice(total))
times.push(total)
if(total > options.maxtime*1000){
break
}
}
var sum = times.reduce(function(a, b) { return a + b; });
var avg = sum / times.length;
var med = median(times)
var r = {
avg:avg,
min:Math.min.apply(null, times),
max:Math.max.apply(null, times),
times:times,
median:med,
sampled:i,
}
logtitle(name+'\n median '+milli2nice(med)+'\n max='+milli2nice(r.max)+' min='+milli2nice(r.min)+'\n values: '+times.map(milli2nice).slice(0,10).join(', '))
return r;
}
setTimeout(function(){
to_test = function(){
S.refresh({skipIndexation:true});
}
S.camera.settings('font','robotoregular')
var res2 = bench('hot hot hot',to_test);
S.camera.settings('font','Anonymous Pro')
var res2 = bench('Anonymous Pro',to_test);
S.camera.settings('font','robotoregular')
var res2 = bench('roboto',to_test);
S.camera.settings('font','arial')
var res2 = bench('arial',to_test);
},0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment