1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function compare(fn1,fn2){
var MAX_DELAY = 20,
NBR_TEST = 9;
//
var tab1=[],tab2=[],iter=1,delay=0,median=Math.round(NBR_TEST /2),time;
while(delay<MAX_DELAY){
time=new Date().getTime();
for(var j=(iter*=2);j>0;j--){
fn1();
fn2();
}
delay = (new Date().getTime())-time;
}
for(;NBR_TEST>0;NBR_TEST--){
// test fn1
time = new Date().getTime();
for(var j=iter;j>0;j--)
fn1();
tab1.push((new Date().getTime())-time);
// test fn2
time = new Date().getTime();
for(var j=iter;j>0;j--)
fn2();
tab2.push((new Date().getTime())-time);
}
tab1.sort(function(a,b){return a-b;});
tab2.sort(function(a,b){return a-b;});
return (tab1[median]/tab2[median]);
}
// test1
function f1(u){
for(var i=0;i<1 && !u;i++);
}
function f2(u){
for(var i=0;i<1 && u==undefined;i++);
}
alert("ration (!u)/(u==undefined) : "+Math.round(compare(f1,f2)*100)+"% (1/ration = "+Math.round(compare(f2,f1)*100)+"%)");
// test2
function f11(u){
for(var i=0;i<10000 && !u;i++);
}
function f22(u){
for(var i=0;i<10000 && u==undefined;i++);
}
alert("ration (!u)/(u==undefined) : "+Math.round(compare(f11,f22)*100)+"% (1/ration = "+Math.round(compare(f22,f11)*100)+"%)"); |
Partager