Is it inefficient to pass the whole model from function to function?

Many thanks for all these excellent replies. Been trying to get my head round it all.
I’ve marked further questions and assertions with bold numbering.


Nope, that’s exactly what I meant; you got me first time :slight_smile: Very clear explanation, thank you.


@Atlewee Brilliant — I’d forgotten Html.lazy.

1) So, in the view, we need to avoid passing references to stuff that changes frequently — whether using lazy or not — because the DOM is updated every time that something passed to a view function changes, whether relevant to the function or not. And we then use lazy where input values change rarely, to avoid unnecessary virtual DOM updates as well.

2) So, would it be true to say that, Html.lazy viewFunction model, is the worst of all worlds? Because Html.lazy has to build a model sized ‘memo’ that will never be used because there’s always something changing in the model?


3) So, in

viewFunction : Model -> Html Msg
viewFunction model =

we are not actually passing model to the function, but instead we are making ‘model’ available to the function.

Then, by using an extensible record (great description here) the function definition limits what it can see/access, and therefore what it is responsible for: if a function can’t access a value then

  • it can’t be responsible for errors in the value, and
  • the value can’t be (and isn’t required to be) part of the function’s tests

4) The consensus seems to be that, generally, passing around a reference to the whole model isn’t a problem, but that it’s better to have functions that reference only values that they use so as to limit their scope of responsibility and make them easier to test, and easier to re-use.


5) My original question stemmed principally from view functions.

If we have a deep view structure like this:

         mainView
            |
  -------------------------------
  |         |         |         |
view1     view2     view5     view6 
  |         |         |  
view3     view4     view7  
            |
          view8

The function view2 must carry references to everything required by view4 and view8. So, for simplicity, we end up passing a reference to the whole model down each branch from function to function.

Given that we should avoid passing more to a function than it needs for it’s own purposes, this suggests that a flat view structure is desirable.

         mainView
            |
  --------------------------------------------------------------------
  |         |         |         |         |        |        |        | 
view1     view2     view3     view4     view5    view6    view7    view8

6) An interesting question, if I understand it correctly.

So, if we have a model that is { field1 : Int, field2 : String, fieldTimePulse : Posix }
and a function defined as viewFunction model = ...
and we call it with viewFunction model
any change in any value in model will cause a recalculation (virtual DOM) and repaint (DOM) of viewFunction.

But what happens if we call it with
viewFunction { model | field1 : Int, field2 : String }
or with
lazy viewFunction { model | field1 : Int, field2 : String }
Are either/both of these function calls affected by the multiple changes in fieldTimePulse ?

1 Like