Skip to content

Instantly share code, notes, and snippets.

@esprehn
Created March 8, 2016 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esprehn/718242dbcae337d8c67d to your computer and use it in GitHub Desktop.
Save esprehn/718242dbcae337d8c67d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<body>
<template id="template">
<style>
:host { color: red; display: block; }
</style>
<div>Example</div>
</template>
<script>
var template = document.getElementById("template");
function createdCallback() {
/*
NEVER INLINE.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
*/
this.createShadowRoot().appendChild(document.importNode(template, true));
}
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = createdCallback;
var XRealElement = document.registerElement("x-real-element", { prototype: proto });
var kElementCount = 10000;
function noCustomElement() {
var text = "";
for (var i = 0; i < kElementCount; ++i) {
text += "<x-fake-element></x-fake-element>";
}
var t = performance.now();
var widget = document.createElement("div");
widget.innerHTML = text;
for (var child = widget.firstElementChild; child; child = child.nextElementSibling) {
createdCallback.call(child);
}
document.body.appendChild(widget);
console.log("noCustomElement", performance.now() - t);
}
function realCustomElement() {
var t = performance.now();
var widget = document.createElement("div");
for (var i = 0; i < kElementCount; ++i) {
var element = document.createElement("x-real-element");
widget.appendChild(element);
}
document.body.appendChild(widget);
console.log("realCustomElement", performance.now() - t);
}
function fakeCustomElement() {
var t = performance.now();
var widget = document.createElement("div");
for (var i = 0; i < kElementCount; ++i) {
var element = document.createElement("x-fake-element");
createdCallback.call(element);
widget.appendChild(element);
}
document.body.appendChild(widget);
console.log("fakeCustomElement", performance.now() - t);
}
function fakeCustomElementWithProto() {
var t = performance.now();
var widget = document.createElement("div");
for (var i = 0; i < kElementCount; ++i) {
var element = document.createElement("x-fake-element");
element.__proto__ = XRealElement.prototype;
createdCallback.call(element);
widget.appendChild(element);
}
document.body.appendChild(widget);
console.log("fakeCustomElementWithProto", performance.now() - t);
}
function fakeCustomElementMoreCrossing() {
var t = performance.now();
var widget = document.createElement("div");
for (var i = 0; i < kElementCount; ++i) {
var element = document.createElement("x-fake-element");
createdCallback.call(element);
var noop = element.shadowRoot.getElementById("test");
widget.appendChild(element);
}
document.body.appendChild(widget);
console.log("fakeCustomElementMoreCrossing", performance.now() - t);
}
noCustomElement();
fakeCustomElement();
realCustomElement();
fakeCustomElementWithProto();
fakeCustomElementMoreCrossing();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment