Skip to content

Instantly share code, notes, and snippets.

@zentooo
Created June 5, 2011 13:17
Show Gist options
  • Save zentooo/1008954 to your computer and use it in GitHub Desktop.
Save zentooo/1008954 to your computer and use it in GitHub Desktop.
benchmark for Array.prototype.forEach and for-loop
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
html {
font-size: 200%;
}
</style>
<script type="text/javascript" src="http://github.com/bestiejs/benchmark.js/raw/master/benchmark.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var suite = new Benchmark.Suite,
body = document.body,
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
suite.add('Array.prototype.forEach', function() {
var sum = 0;
array.forEach(function(num) {
sum += num;
});
})
.add('for loop', function() {
var i = 0, j = array.length, sum = 0;
for ( ; i < j; i++ ) {
sum += array[i];
}
})
// add listeners
.on('cycle', function(bench) {
body.innerHTML += (String(bench)) + "<br />";
})
// run async
.run(true);
}, false);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment