I’ve tried to understand better why should one (or shouldn’t) inherit from Object (var o = Object.create(null);). If I’ve got the answer right, performance reasons seemed to be the main viable reason to “inherit” from null.
So, I wanted to check it (utilizing small, handy and cute profiler called JSLitmus):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<script src="JSLitmus.js"></script>
<script>
JSLitmus.test('Prototypeless test', function() {
var o1 = Object.create(null);
o1.foo = "bar";
for (i = 0; i < 1000000; ++i) {
o1.foo;
};
});
JSLitmus.test('Prototypeful test', function() {
var o2 = {};
o2.foo = "bar";
for (i = 0; i < 1000000; ++i) {
o2.foo;
};
});
</script>
</head>
<body>
</body>
</html>
When executed, I’ve got the same (controversial) results. Does it mean that there is no performance penalty when inheriting from Object?
I’ve reconsidered my own code and slightly modified the code to make the
foofield lookup to propagate through the “inheritance” chain:In this way, I’ve got 10% better results when accessing the
o1property.