Skip to content

Instantly share code, notes, and snippets.

@crazyjat
Last active December 2, 2016 03:50
Show Gist options
  • Save crazyjat/41d155dbe96cabf1ce00b2944260f3e4 to your computer and use it in GitHub Desktop.
Save crazyjat/41d155dbe96cabf1ce00b2944260f3e4 to your computer and use it in GitHub Desktop.
My test of prototype vs object return #jsbench #jsperf (http://jsbench.github.io/#41d155dbe96cabf1ce00b2944260f3e4) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>My test of prototype vs object return #jsbench #jsperf</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;
suite.add("(function() {", function () {
(function() {
var controller = function() {
var data = {
message: "I am a banana",
};
return {
setMsg: function(msg) {
data.message = msg;
},
getMsg: function() {
return data.message;
},
};
};
for (let i = 0; i < 100; ++i) {
var inst = new controller();
inst.setMsg("I farted!");
console.log(inst.getMsg());
var inst2 = new controller();
console.log(inst2.getMsg());
inst2.setMsg("Hello world");
console.log(inst.getMsg());
console.log(inst2.getMsg());
}
})();
});
suite.add("(function() {", function () {
(function() {
const controller = function() {
const ctrl = {
message: "I am a banana",
};
function setMsg(msg) {
ctrl.message = msg;
}
function getMsg() {
setMsg(ctrl.message + " bees");
return ctrl.message;
}
return {
setMsg,
getMsg,
};
};
for (let i = 0; i < 100; ++i) {
var inst = new controller();
inst.setMsg("I farted!");
console.log(inst.getMsg());
var inst2 = new controller();
console.log(inst2.getMsg());
inst2.setMsg("Hello world");
console.log(inst.getMsg());
console.log(inst2.getMsg());
}
})();
});
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("My test of prototype vs object return #jsbench #jsperf");
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