Skip to content

Instantly share code, notes, and snippets.

@evensandbox
Created February 27, 2014 02:48
Show Gist options
  • Select an option

  • Save evensandbox/9243378 to your computer and use it in GitHub Desktop.

Select an option

Save evensandbox/9243378 to your computer and use it in GitHub Desktop.
function f1() {
var count = 1;
var last = Date.now();
var ret = [];
setTimeout(function() {
ret.push( Date.now() - last );
var i = 0, c;
while ( i++ < 100000 ) {
c = document.getElementsByTagName( 'a' );
}
if ( count++ > 100 ) {
console.log( ret );
return;
}
else {
last = Date.now();
setTimeout( arguments.callee, 10 );
}
}, 10);
}
function f2() {
var count = 1;
var last = Date.now();
var ret = [];
var inter = setInterval(function() {
ret.push( Date.now() - last );
var i = 0, c;
while ( i++ < 100000 ) {
c = document.getElementsByTagName( 'a' );
}
if ( count++ > 100 ) {
clearInterval( inter );
console.log( ret );
}
else last = Date.now();
}, 10);
}
// 10s 时间内执行了多少次.
function f3() {
var time = 10 * 1000; // 10s
var last = Date.now();
var count = 0;
setTimeout(function() {
if ( Date.now() - last > time ) {
console.log( 'f3:', count );
return;
}
count++;
var i = 0, c;
while ( i++ < 100000 ) {
c = document.getElementsByTagName( 'a' );
}
setTimeout( arguments.callee, 10 );
}, 10);
}
function f4() {
var count = 0;
var time = 10 * 1000;
var last = Date.now();
var inter = setInterval(function() {
if ( Date.now() - last > time ) {
clearInterval( inter );
console.log( 'f4: ', count );
return;
}
count++;
var i = 0, c;
while ( i++ < 100000 ) {
c = document.getElementsByTagName( 'a' );
}
}, 10);
}
f1();
f2();
f3();
f4();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment