Skip to content

Instantly share code, notes, and snippets.

@msingleton
Created April 13, 2011 21:38
Show Gist options
  • Save msingleton/918482 to your computer and use it in GitHub Desktop.
Save msingleton/918482 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Array Length Caching</title>
<script type="text/javascript">
// Generate huge array
var bigArray = [];
for(var i = 0; i < 10000000; i++) {
bigArray.push(1);
}
function slowLoop() {
var start = (new Date()).getTime();
for(var i = 0; i < bigArray.length; i++) {
// do something
}
var diff = (new Date()).getTime() - start;
console.log(diff);
}
function fastLoop() {
var start = (new Date()).getTime();
for(var i = 0, l = bigArray.length; i < l; i++ ) {
// do something
}
var diff = (new Date()).getTime() - start;
console.log(diff);
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment