Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.setPrototypeOf() is a performance killer #1

Open
jlgrall opened this issue Feb 26, 2018 · 1 comment
Open

Object.setPrototypeOf() is a performance killer #1

jlgrall opened this issue Feb 26, 2018 · 1 comment

Comments

@jlgrall
Copy link

jlgrall commented Feb 26, 2018

core/src/registerRoot.js

Lines 28 to 29 in 12250ef

Object.setPrototypeOf(proto, parentProto)
Object.setPrototypeOf(RootElement, parentConstructor)

Hi, I read the article Writing a JavaScript Framework - The Benefits of Custom Elements. And I have been thinking about the use of Object.setPrototypeOf() which is a performance killer.

Why could you not use Object.create() ? What would be missing if you used Object.create()?

@jlgrall
Copy link
Author

jlgrall commented Mar 31, 2018

Maybe with something like this you could get rid of the Object.setPrototypeOf() and still have the right constructor be executed on instanciations:

var parent = HTMLElement;
var RootElement = function() {
    console.log("Executing RootElement ctor.");
    return Reflect.construct(HTMLElement.constructor, [], RootElement);
};

// Used to make the new class extends its parent:
var protoChainingProxy = function() { }; // This function can actually be reused or cached, it doesn't have to be re-created each time.
// Prepare prototype chaining to the parent:
protoChainingProxy.prototype = parent.prototype;
// Make the prototype's chain from RootElement to it's parent:
var proto = RootElement.prototype = new protoChainingProxy;
proto.constructor = RootElement;	// This is not required, but is good conduct and might be expected from other libs

console.log(RootElement);
console.log(new RootElement());
console.log(new RootElement() instanceof HTMLElement); // true

Copy-paste in your js console to test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant