Skip to content

Instantly share code, notes, and snippets.

@jerch
Created May 13, 2018 12:31
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 jerch/152aede83d156b82bba983cf4141f60e to your computer and use it in GitHub Desktop.
Save jerch/152aede83d156b82bba983cf4141f60e to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#152aede83d156b82bba983cf4141f60e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Untitled benchmark</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 letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10};
var no_prototype = Object.create(null);
for (var i=0; i<letters.length; ++i) {
no_prototype[letters[i]] = i+1;
}
function cb(result) {
return result;
}
};
suite.add("for (var i=0; i<letters.length; ++i) {", function () {
for (var i=0; i<letters.length; ++i) {
cb(obj[letters[i]]);
}
});
suite.add("for (var i=0; i<letters.length; ++i) {", function () {
for (var i=0; i<letters.length; ++i) {
cb(no_prototype[letters[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("Untitled benchmark");
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