Skip to content

Instantly share code, notes, and snippets.

@Infocatcher
Last active August 29, 2015 14:02
Show Gist options
  • Save Infocatcher/3502f9c9b858028520ca to your computer and use it in GitHub Desktop.
Save Infocatcher/3502f9c9b858028520ca to your computer and use it in GitHub Desktop.
__proto__ vs usable Object.create() wrapper
var c = 100e3;
var proto = {
a: 0,
b: {},
c: [],
d: function() {},
e: /./
};
var i = c + 1, t = performance.now();
while(--i) {
var o = inherit(proto, {
f: true,
g: "test",
h: function() {}
});
}
var dt1 = performance.now() - t;
var i = c + 1, t = performance.now();
while(--i) {
var o = {
__proto__: proto,
f: true,
g: "test",
h: function() {}
};
}
var dt2 = performance.now() - t;
var i = c + 1, t = performance.now();
while(--i) {
var o = ensurePrototype({
__proto__: proto,
f: true,
g: "test",
h: function() {}
});
}
var dt3 = performance.now() - t;
var i = c + 1, t = performance.now();
while(--i) {
var o = ensurePrototype$({
__proto__$: proto,
f: true,
g: "test",
h: function() {}
});
}
var dt4 = performance.now() - t;
alert(
"inherit(): " + dt1.toFixed(2) + " ms\n" +
"__proto__: " + dt2.toFixed(2) + " ms\n" +
"ensurePrototype(): " + dt3.toFixed(2) + " ms\n" +
"ensurePrototype() w/o __proto__: " + dt4.toFixed(2) + " ms"
);
// https://github.com/piroor/fxaddonlib-inherit
function toPropertyDescriptors(aProperties) {
var descriptors = {};
Object.keys(aProperties).forEach(function(aProperty) {
var description = Object.getOwnPropertyDescriptor(aProperties, aProperty);
descriptors[aProperty] = description;
});
return descriptors;
}
function inherit(aParent, aExtraProperties) {
var global;
if (Components.utils.getGlobalForObject)
global = Components.utils.getGlobalForObject(aParent);
else
global = aParent.valueOf.call();
global = global || this;
var ObjectClass = global.Object || Object;
if (!ObjectClass.create) {
aExtraProperties = aExtraProperties || new ObjectClass;
aExtraProperties.__proto__ = aParent;
return aExtraProperties;
}
if (aExtraProperties)
return ObjectClass.create(aParent, toPropertyDescriptors(aExtraProperties));
else
return ObjectClass.create(aParent);
}
function ensurePrototype(o) {
ensurePrototype = "hasOwnProperty" in ({ __proto__: null })
? function(o) {
var proto = o.__proto__;
delete o.__proto__;
var global;
if(Components.utils.getGlobalForObject)
global = Components.utils.getGlobalForObject(o);
else
global = o.valueOf.call();
if(!global)
global = this;
var ObjectClass = global.Object || Object;
var descs = {};
Object.keys(o).forEach(function(p) {
descs[p] = Object.getOwnPropertyDescriptor(o, p);
});
return ObjectClass.create(proto, descs);
}
: function(o) {
return o;
};
return ensurePrototype(o);
}
function ensurePrototype$(o) { // To test performance w/o __proto__ support
ensurePrototype$ = "hasOwnProperty" in ({ __proto__$: null })
? function(o) {
var proto = o.__proto__$;
delete o.__proto__$;
var global;
if(Components.utils.getGlobalForObject)
global = Components.utils.getGlobalForObject(o);
else
global = o.valueOf.call();
if(!global)
global = this;
var ObjectClass = global.Object || Object;
var descs = {};
Object.keys(o).forEach(function(p) {
descs[p] = Object.getOwnPropertyDescriptor(o, p);
});
return ObjectClass.create(proto, descs);
}
: function(o) {
return o;
};
return ensurePrototype$(o);
}
@Infocatcher
Copy link
Author

For me (Firefox 29.0.1):

inherit(): 2246.00 ms
__proto__: 187.00 ms
ensurePrototype(): 203.00 ms
ensurePrototype() w/o __proto__: 2434.00 ms

:(

@Infocatcher
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment