Skip to content

Commit 383754d

Browse files
committed
Demo1 --> Demo2
1 parent b713c43 commit 383754d

File tree

8 files changed

+7
-7
lines changed

8 files changed

+7
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ 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 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)
74+
## Demo 2
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/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)
7777

7878
(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).
7979

8080
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.
8181
This eliminates any concerns about a a delay in the `fetch()` as outlined above. However, in addition to the clunky developer ergonimics
8282
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-
[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:
84+
[Demo 2](https://dandclark.github.io/json-css-module-notes/demo2/index.html) 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:
8585

86-
![Memory savings with CSS modules versus inlined CSS-as-JS-string](demo1/demo1SteadyState.PNG)
86+
![Memory savings with CSS modules versus inlined CSS-as-JS-string](demo2/steadyState.PNG)
8787

8888
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.
8989

@@ -99,11 +99,11 @@ This demo compares two similar custom elements written as a JavaScript module, e
9999
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.
100100

101101
#### With `<link rel="stylesheet">` in shadowRoot:
102-
![With <link rel="stylesheet"> in shadowRoot](demo3NoModule.PNG)
102+
![With <link rel="stylesheet"> in shadowRoot](demo3/noModule.PNG)
103103

104104
Using CSS modules, styles.css is fetched as part of processing the module graph, before any JavaScript is executed.
105105

106106
#### With CSS module:
107-
![With CSSmodule](demo3Module.PNG)
107+
![With CSSmodule](demo3/module.PNG)
108108

109109
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)