Skip to content

Instantly share code, notes, and snippets.

@micnic
Last active February 18, 2021 11:33
Show Gist options
  • Save micnic/fe2c48dab56529fa112b30c5aaa257bb to your computer and use it in GitHub Desktop.
Save micnic/fe2c48dab56529fa112b30c5aaa257bb to your computer and use it in GitHub Desktop.
Object.create() vs object literal #jsbench #jsperf (http://jsbench.github.io/#fe2c48dab56529fa112b30c5aaa257bb) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Object.create() vs object literal #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;
Benchmark.prototype.setup = function () {
const { hasOwnProperty } = Object.prototype;
const loopObject = (obj) => {
const result = [];
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
result.push(key);
}
}
return result;
};
const loopObjectHOPMethod = (obj) => {
const result = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(key);
}
}
return result;
};
const loopObjectHOPChached = (obj) => {
const result = [];
for (const key in obj) {
if (hasOwnProperty.call(obj, key)) {
result.push(key);
}
}
return result;
};
const loopObjectHOPChachedBinded = (obj) => {
const result = [];
const hasProperty = hasOwnProperty.bind(obj);
for (const key in obj) {
if (hasProperty(key)) {
result.push(key);
}
}
return result;
};
};
suite.add("loopObject({});", function () {
loopObject({});
});
suite.add("loopObjectHOPChached({});", function () {
loopObjectHOPChached({});
});
suite.add("loopObjectHOPChachedBinded({});", function () {
loopObjectHOPChachedBinded({});
});
suite.add("loopObject({ a: 0 });", function () {
loopObject({ a: 0 });
});
suite.add("loopObjectHOPChached({ a: 0 });", function () {
loopObjectHOPChached({ a: 0 });
});
suite.add("loopObjectHOPChachedBinded({ a: 0 });", function () {
loopObjectHOPChachedBinded({ a: 0 });
});
suite.add("loopObject({ a: 0, b: 1 });", function () {
loopObject({ a: 0, b: 1 });
});
suite.add("loopObjectHOPChached({ a: 0, b: 1 });", function () {
loopObjectHOPChached({ a: 0, b: 1 });
});
suite.add("loopObjectHOPChachedBinded({ a: 0, b: 1 });", function () {
loopObjectHOPChachedBinded({ a: 0, b: 1 });
});
suite.add("loopObjectHOPMethod({ a: 0, b: 1 });", function () {
loopObjectHOPMethod({ a: 0, b: 1 });
});
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("Object.create() vs object literal #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