Skip to content

Instantly share code, notes, and snippets.

@jeremyckahn
Created September 16, 2010 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyckahn/581813 to your computer and use it in GitHub Desktop.
Save jeremyckahn/581813 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript">
// inits
var loopLimit = 100000000,
countLimit = 5,
wait = 500;
// Pointless function that exists only to waste time/CPU cycles.
function countTo(limit){
var start = new Date();
for (var i = 0; i < limit; i++){
var a = true;
}
write('countTo() completed in ' + (new Date() - start) + ' milliseconds.');
return;
}
function write(str){
document.getElementsByTagName('body')[0].innerHTML += str + '<br/>'
}
// Shows how setInterval works.
function testInterval(callback){
var count = 0,
start = new Date();
timeoutHandle = setInterval(function(){
if (count++ >= countLimit){
// Cancel our timeout, we don't want it to fire anymore.
clearInterval(timeoutHandle);
write('<b>--testInterval() completed in ' + (new Date() - start) + ' milliseconds.<br/></b>');
callback && callback();
return;
}
countTo(loopLimit)
}, wait);
}
// Shows how setTimeout works.
function testTimeout(callback){
var count = 0,
start = new Date();
setTimeout(function(){
if (count++ >= countLimit){
write('<b>--testTimeout() completed in ' + (new Date() - start) + ' milliseconds.</b><br/>');
callback && callback();
return;
}
countTo(loopLimit)
setTimeout(arguments.callee, wait)
}, wait);
}
</script>
</head>
<body>
<script type="text/javascript">
write('Counting to ' + loopLimit + ' (this is the long running block of code).')
write('The for loop is being run ' + countLimit + ' times for each test.');
write('Each timer has a ' + wait + ' millisecond wait time.</br>');
testInterval(testTimeout);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment