Skip to content

Commit 794f3eb

Browse files
authored
Add common optimization to Elm 0.18
**Summary:** This change adds the equivalent of React's `shouldComponentUpdate` for the Elm implementation. **Justification 1:** I noticed that this optimization was permitted in the following React implementations. - [react-v15.5.4-keyed](https://github.com/krausest/js-framework-benchmark/blob/master/react-v15.5.4-keyed/src/Row.jsx#L15-L17) - [react-v15.5.4-redux-v3.6.0](https://github.com/krausest/js-framework-benchmark/blob/master/react-v15.5.4-redux-v3.6.0/src/controller.jsx#L60-L62) - [react-lite-v0.15.30](https://github.com/krausest/js-framework-benchmark/blob/master/react-lite-v0.15.30/src/Row.jsx#L15-L17) There are probably others that use similar techniques, but I am not familiar with all of these projects. **Justification 2:** This technique is very commonly used in Elm applications. It is used [here](https://github.com/evancz/elm-todomvc/blob/master/Todo.elm#L300-L306) in our TodoMVC implementation. It has the same role as `shouldComponentUpdate` in optimizing an Elm program. **Expectations:** I am not certain how to get all these benchmarks set up, but I would expect this to (1) reduce the memory footprint and (2) increase speed. For example, when rows are swapped, rather than allocating and diffing the entire `tr` virtual DOM, we will just compare the `row` value itself. This should result in faster updates and less garbage generated. Thank you for your consideration!
1 parent d3f686c commit 794f3eb

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

elm-v0.18.0/src/Main.elm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Html exposing (Html, Attribute, program, div, a, h1, span, button, table,
66
import Html.Attributes exposing (id, class, classList, attribute, type_, href)
77
import Html.Events exposing (onClick)
88
import Html.Keyed
9+
import Html.Lazy
910
import String
1011
import Random.Pcg exposing (Seed, Generator)
1112

@@ -115,10 +116,14 @@ btnPrimaryBlock ( buttonId, labelText, msg ) =
115116
]
116117

117118

118-
rowView : Int -> Row -> ( String, Html Msg )
119-
rowView index { id, label, selected } =
120-
( toString id
121-
, tr
119+
viewKeyedRow : Int -> Row -> ( String, Html Msg )
120+
viewKeyedRow index row =
121+
( toString row.id, Html.Lazy.lazy2 viewRow index row )
122+
123+
124+
viewRow : Int -> Row -> Html Msg
125+
viewRow index { id, label, selected } =
126+
tr
122127
[ classList [ ( "danger", selected ) ] ]
123128
[ td [ class "col-md-1" ] [ text (toString id) ]
124129
, td
@@ -144,7 +149,6 @@ rowView index { id, label, selected } =
144149
]
145150
, td [ class "col-md-6" ] []
146151
]
147-
)
148152

149153

150154
view : Model -> Html Msg
@@ -170,7 +174,7 @@ view model =
170174
[ class "table table-hover table-striped test-data" ]
171175
[ tbody
172176
[]
173-
(List.indexedMap rowView model.rows)
177+
(List.indexedMap viewKeyedRow model.rows)
174178
]
175179
, span
176180
[ class "preloadicon glyphicon glyphicon-remove"

0 commit comments

Comments
 (0)