Last active
August 5, 2020 21:39
-
-
Save jridgewell/0bd7efc7c75ef828464d8428c3535a67 to your computer and use it in GitHub Desktop.
Adopted vs legacy stylesheets (https://jsbench.github.io/#0bd7efc7c75ef828464d8428c3535a67) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"/> | |
| <title>Adopted vs legacy stylesheets</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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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 () { | |
| var container = document.createElement('div'); | |
| document.body.appendChild(container); | |
| var host = document.createElement('div'); | |
| container.appendChild(host); | |
| var shadowRoot = host.attachShadow({mode: 'open'}); | |
| var CSS = '.class1 {background: red; width: 100px; height: 100px}'; | |
| function uniqueCss() { | |
| return CSS.replace('red', 'rgba(' + | |
| (Math.floor(Math.random() * 255)) + | |
| ',' + | |
| (Math.floor(Math.random() * 255)) + | |
| ',' + | |
| (Math.floor(Math.random() * 255)) + | |
| ')'); | |
| } | |
| if (!window.cachedStyleSheet) { | |
| window.cachedStyleSheet = new CSSStyleSheet(); | |
| window.cachedStyleSheet.replaceSync(CSS); | |
| window.cachedBlobUrl = URL.createObjectURL(new Blob([CSS], { type: 'text/css' })); | |
| } | |
| function addContent() { | |
| var content = document.createElement('div'); | |
| content.className = 'class1'; | |
| shadowRoot.appendChild(content); | |
| // var bg = getComputedStyle(content).backgroundColor; | |
| // if (bg !== 'rgb(255, 0, 0)' && bg !== 'red') { | |
| // throw new Error('not red'); | |
| // } | |
| } | |
| }; | |
| Benchmark.prototype.teardown = function () { | |
| container.textContent = ''; | |
| }; | |
| suite.add("Legacy stylesheet", function () { | |
| // Legacy stylesheet | |
| var style = document.createElement('style'); | |
| style.textContent = uniqueCss(); | |
| shadowRoot.appendChild(style); | |
| addContent(); | |
| }); | |
| suite.add("Recreated adopted", function () { | |
| // Recreated adopted | |
| var styleSheet = new CSSStyleSheet(); | |
| styleSheet.replaceSync(uniqueCss()); | |
| shadowRoot.adoptedStyleSheets = [styleSheet]; | |
| addContent(); | |
| }); | |
| suite.add("Cached adopted", function () { | |
| // Cached adopted | |
| shadowRoot.adoptedStyleSheets = [window.cachedStyleSheet]; | |
| addContent(); | |
| }); | |
| suite.add("Cached blob", function () { | |
| // Cached blob | |
| var link = document.createElement('link'); | |
| link.rel = 'stylesheet'; | |
| link.href = window.cachedBlobUrl; | |
| shadowRoot.appendChild(link); | |
| addContent(); | |
| }); | |
| 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("Adopted vs legacy stylesheets"); | |
| 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