Skip to content

Commit 52ea6a9

Browse files
committed
Add explanation of the transformer
1 parent 9b2cc3b commit 52ea6a9

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/transforms/supportArraysForHtml.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
11
/*
22
3-
TODO Docs
3+
## Goal
4+
5+
This transformer aims to avoid unnecessary Elm List to JavaScript Array conversions when it comes to `elm/html` functions.
6+
7+
Here is a small example:
8+
9+
```elm
10+
Html.div [ Html.Attributes.class "some", Html.Attributes.class "class" ] [ a, b, c ]
11+
```
12+
gets compiled to
13+
```js
14+
A2(
15+
$elm$html$Html$div,
16+
_List_fromArray([
17+
$elm$html$Html$Attributes$class("some"),
18+
$elm$html$Html$Attributes$class("class")
19+
]),
20+
_List_fromArray([a, b, c])
21+
);
22+
```
23+
24+
Through `_List_fromArray`, the attributes and children are converted from JavaScript Arrays to Elm Lists.
25+
This conversion has a runtime cost, and Elm lists are slower to iterate through as well.
26+
27+
The idea behind this transformer is to remove these `_List_fromArray` calls and to keep the JavaScript Arrays as such,
28+
and to have the underlying functions able to iterate through JavaScript Arrays as well. Taking the example from before,
29+
the result would end up being:
30+
```js
31+
A2(
32+
$elm$html$Html$div,
33+
[
34+
$elm$html$Html$Attributes$class("some"),
35+
$elm$html$Html$Attributes$class("class")
36+
],
37+
[a, b, c]
38+
);
39+
```
40+
41+
This change will only be applied when the arguments are literal lists, not when they are variables or more complex expressions.
42+
Further work could potentially increase the number of cases that we apply this change.
43+
44+
Elm Lists will have to be supported still, because this transformer will not be able to replace all attributes and children.
45+
46+
## Explanation of the transformer
47+
48+
1. When this transformer is enabled, replacements for `_VirtualDom_nodeNS` and `_VirtualDom_organizeFacts` are introduced.
49+
These replacements will make the 2 functions (which are at the root of the VirtualDom functions) support JavaScript Arrays.
50+
More replacements should probably be added to support SVG and custom nodes.
51+
2. Detect which functions can benefit from this optimization. Currently, it's only the functions that call `_VirtualDom_node`,
52+
but we could extend this further with a bit more analysis.
53+
3. Remove `_List_fromArray` from the arguments from the functions detected in 2.
454
555
*/
656

0 commit comments

Comments
 (0)