Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@reekoheek
Created September 8, 2016 17:11
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 reekoheek/e0c03f3c367adf7c5514b19bd016f9ba to your computer and use it in GitHub Desktop.
Save reekoheek/e0c03f3c367adf7c5514b19bd016f9ba to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>iteration</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
var x = [];
for(var i = 0; i < 100; i++) {
x.push(i);
}
};
suite.add("for(var i in x) {", function () {
for(var i in x) {
var j = x[i];
}
});
suite.add("x.forEach(function(j, i) {", function () {
x.forEach(function(j, i) {
});
});
suite.add("for(var i = 0; i < x.length; i++) {", function () {
for(var i = 0; i < x.length; i++) {
var j = x[i];
}
});
suite.add("var len = x.length;", function () {
var len = x.length;
for(var i = 0; i < len; i++) {
var j = x[i];
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("iteration");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment