Skip to content

Instantly share code, notes, and snippets.

@jaandrle
Created August 23, 2018 09:55
Show Gist options
  • Save jaandrle/2a57560b4d4532d931f97b16c709ea9e to your computer and use it in GitHub Desktop.
Save jaandrle/2a57560b4d4532d931f97b16c709ea9e to your computer and use it in GitHub Desktop.
/*
Final results 3 x test(258) + 3 x test(NodeList(length=258)) - Android7 WebView
Original: for 3x faster than forEach and 10% times is quicker
$iterable2=for inside function
Own vs for (only $iterable2 make sence): for 0.9x slower than $iterable2 and quicker in 3/10 vs 3.333/10
=== All Results - Average ===
Delays: [394.3333333333333, 43.166666666666664, 150.66666666666666, 38.333333333333336]
Winners: ["2.5times", "3times", "2times", "3.3333333333333335times"]
*/
function test(arr_input){
const arr= typeof arr_input === "number" ? arr_init(arr_input) : arr_input;
const tt_empty= ()=>[0,0,0,0];
let ttt_0= tt_empty(), ttt_sum= tt_empty();
let tt= tt_empty();
let t= [];
const loopAdd= (ti,i)=>tt[i]+=ti;
const testFce= ()=>document.querySelectorAll(c_CMD);
function test1(){
t[0]= perf(); $iterable(arr)(testFce); t[0]= t[0]();
}
function test2(){
t[1]= perf(); for(let k=0, k_length= arr.length; k<k_length; k++){ testFce(arr[k]); } t[1]= t[1]();
}
function test3(){
t[2]= perf(); [].forEach.call(arr, testFce); t[2]= t[2]();
}
function test4(){
t[3]= perf(); $iterable2(arr, testFce); t[3]= t[3]();
}
for(let j=0; j<10; j++){
for(let i=0; i<3; i++){ test1(); test2(); test3(); test4(); t.forEach(loopAdd); }
for(let i=0; i<3; i++){ test4(); test1(); test2(); test3(); t.forEach(loopAdd); }
for(let i=0; i<3; i++){ test3(); test4(); test1(); test2(); t.forEach(loopAdd); }
for(let i=0; i<3; i++){ test2(); test3(); test4(); test1(); t.forEach(loopAdd); }
write(...tt);
tt= tt_empty();
}
function $iterable2(iterable, i_function){
//[].forEach.call(iterable, i_function);
for(let i=0, i_length= iterable.length; i<i_length; i++){ i_function(iterable[i],i); }
}
function $iterable(iterable){
const i_length= iterable.length;
return function(i_function){for(let i=0; i<i_length; i++){ i_function(iterable[i],i); }};
}
function write(...data){
//console.log(data);
const min= Math.min(...data);
data= data.map(item=>item-min);
data.forEach(stats);
function stats(item,i){
if(item) ttt_sum[i]+= item;
else ttt_0[i]++;
}
}
function perf(){
const start= d();
return function(){return d()-start;};
function d(){return (new Date()).getTime();}
}
function arr_init(length){
length++;
let out= [];
for(let i=1; i<length; i++){
out.push(i);
}
return out;
}
const min= Math.min(...ttt_sum);
console.log('=== Results ===');
console.log("Delays:", ttt_sum.map(item=>item-min));
console.log('Winners:', ttt_0.map(item=>item+"times"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment