Skip to content

Commit b713c43

Browse files
committed
Demo2 --> Demo3
1 parent 2489915 commit b713c43

File tree

11 files changed

+23
-22
lines changed

11 files changed

+23
-22
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,39 @@ From the perspective of the importer this is ergonomically more or less equivale
7171

7272
And the above depends on the standardization and broad adoption of top-level await. Until that happens, devs are stuck exporting a Promise that the importer needs to deal with.
7373

74-
## Demo 2
75-
### [CSS modules vs `<link>` elements in shadow roots](https://dandclark.github.io/json-css-module-notes/demo2/index.html)
76-
[https://dandclark.github.io/json-css-module-notes/demo2/index.html](https://dandclark.github.io/json-css-module-notes/demo2/index.html)
74+
## Demo 1
75+
### [CSS/JSON modules have a lower memory footprint than inlining the CSS/JSON as a JavaScript string](https://dandclark.github.io/json-css-module-notes/demo1/index.html)
76+
[https://dandclark.github.io/json-css-module-notes/demo1/index.html](https://dandclark.github.io/json-css-module-notes/demo1/index.html)
7777

78-
(There is no general CSS modules browser support as of this writing; that part of the demo was created and tested using a custom Chromium build).
79-
80-
This demo compares two similar custom elements written as a JavaScript module, each of which loads its styles from a separate `styles.css` file. The first custom element applies its styles by adding the styles via a `<link rel="stylesheet">` in the custom element shadow root. The second loads its styles via a CSS module.
78+
(There is no general CSS modules browser support as of this writing; that part of the demo was created and tested a custom Chromium build).
8179

82-
With the `<link>` element approach, the `<link>` isn't processed until an instance of the custom element is inserted into the document. In [demo2/NoModule.html](demo2/noModule.html), there is a delay before an instance of the custom element is created and inserted (to simulate, for example, a custom element that is only added based on some user action) and thus a corresponding delay before styles.css is fetched.
80+
An alternative non-module approach for packaging CSS/JSON in a custom element is to inline the content as a JavaScript string rather than `fetch()`ing it dynamically. This string can be fed into a [Constructed Stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) or a `<style>` element.
81+
This eliminates any concerns about a a delay in the `fetch()` as outlined above. However, in addition to the clunky developer ergonimics
82+
of a bunch of inlined JavaScript string content in one's custom element JS logic, this approach has a quantifiable memory cost. This is due to the fact that the original JS string lives on alongside the CSSStyleSheet or JSON object that it is eventually parsed into. Whereas with CSS/JSON modules, nothing persists but the CSSStylesheet or JSON object.
8383

84-
#### With `<link rel="stylesheet">` in shadowRoot:
85-
![With <link rel="stylesheet"> in shadowRoot](demo2NoModule.PNG)
84+
[Demo1](https://dandclark.github.io/json-css-module-notes/demo1/index.html) linked above illustrates this difference. Both the no-module and the module case load a custom element that pulls in ~30MB of CSS. The no-module case imports inlines it in the JS file defining the custom element, in the style of some of the [existing Chromium layered API elements](https://cs.chromium.org/chromium/src/third_party/blink/renderer/core/script/resources/layered_api/elements/). The module case imports the same CSS as a CSS module. After reaching steady-state, the memory difference is around the same ~30MB as the raw CSS text:
8685

87-
Using CSS modules, styles.css is fetched as part of processing the module graph, before any JavaScript is executed.
86+
![Memory savings with CSS modules versus inlined CSS-as-JS-string](demo1/demo1SteadyState.PNG)
8887

89-
#### With CSS module:
90-
![With CSSmodule](demo2Module.PNG)
88+
This steady state is reached after leaving both tabs in the background for ~60 seconds. Before this final garbage collection, the difference is even more stark; in my observations the inline CSS case hovers around ~95MB for the first 60 seconds, whereas the CSS modules tab goes down to ~38MB within the first few seconds.
9189

92-
If `styles.css` was slow to arrive over the network, or was large enough to take a nontrivial amount of time to parse, front-loading the work could result in a user-percievable difference in how early the styles are applied to the page.
9390

9491
## Demo 3
95-
### [CSS/JSON modules have a lower memory footprint than inlining the CSS/JSON as a JavaScript string](https://dandclark.github.io/json-css-module-notes/demo3/index.html)
92+
### [CSS modules vs `<link>` elements in shadow roots](https://dandclark.github.io/json-css-module-notes/demo3/index.html)
9693
[https://dandclark.github.io/json-css-module-notes/demo3/index.html](https://dandclark.github.io/json-css-module-notes/demo3/index.html)
9794

98-
(There is no general CSS modules browser support as of this writing; that part of the demo was created and tested a custom Chromium build).
95+
(There is no general CSS modules browser support as of this writing; that part of the demo was created and tested using a custom Chromium build).
9996

100-
An alternative non-module approach for packaging CSS/JSON in a custom element is to inline the content as a JavaScript string rather than `fetch()`ing it dynamically. This string can be fed into a [Constructed Stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) or a `<style>` element.
101-
This eliminates any concerns about a a delay in the `fetch()` as outlined above. However, in addition to the clunky developer ergonimics
102-
of a bunch of inlined JavaScript string content in one's custom element JS logic, this approach has a quantifiable memory cost. This is due to the fact that the original JS string lives on alongside the CSSStyleSheet or JSON object that it is eventually parsed into. Whereas with CSS/JSON modules, nothing persists but the CSSStylesheet or JSON object.
97+
This demo compares two similar custom elements written as a JavaScript module, each of which loads its styles from a separate `styles.css` file. The first custom element applies its styles by adding the styles via a `<link rel="stylesheet">` in the custom element shadow root. The second loads its styles via a CSS module.
10398

104-
[Demo3](https://dandclark.github.io/json-css-module-notes/demo3/index.html) linked above illustrates this difference. Both the no-module and the module case load a custom element that pulls in ~30MB of CSS. The no-module case imports inlines it in the JS file defining the custom element, in the style of some of the [existing Chromium layered API elements](https://cs.chromium.org/chromium/src/third_party/blink/renderer/core/script/resources/layered_api/elements/). The module case imports the same CSS as a CSS module. After reaching steady-state, the memory difference is around the same ~30MB as the raw CSS text:
99+
With the `<link>` element approach, the `<link>` isn't processed until an instance of the custom element is inserted into the document. In [demo3/NoModule.html](demo3/noModule.html), there is a delay before an instance of the custom element is created and inserted (to simulate, for example, a custom element that is only added based on some user action) and thus a corresponding delay before styles.css is fetched.
105100

106-
![Memory savings with CSS modules versus inlined CSS-as-JS-string](demo3SteadyState.PNG)
101+
#### With `<link rel="stylesheet">` in shadowRoot:
102+
![With <link rel="stylesheet"> in shadowRoot](demo3NoModule.PNG)
107103

108-
This steady state is reached after leaving both tabs in the background for ~60 seconds. Before this final garbage collection, the difference is even more stark; in my observations the inline CSS case hovers around ~95MB for the first 60 seconds, whereas the CSS modules tab goes down to ~38MB within the first few seconds.
104+
Using CSS modules, styles.css is fetched as part of processing the module graph, before any JavaScript is executed.
105+
106+
#### With CSS module:
107+
![With CSSmodule](demo3Module.PNG)
108+
109+
If `styles.css` was slow to arrive over the network, or was large enough to take a nontrivial amount of time to parse, front-loading the work could result in a user-percievable difference in how early the styles are applied to the page.
File renamed without changes.
File renamed without changes.
File renamed without changes.

demo2/index.html renamed to demo3/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h2>Demo 1: Fetching of JSON and CSS modules starts before script execution</h2>
1+
<h2>Demo: Fetching of JSON and CSS modules starts before script execution</h2>
22
<br>
33
<ul>
44
<li><a href="./noModule.html">With &lt;link rel="stylesheet"&gt; in shadow root</a></li>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)