MVCoconut / coconut.ui

Wow, such reactive view! Much awesome!
The Unlicense
89 stars 9 forks source link

Avoid recreating anonymous functions. #12

Closed back2dos closed 4 years ago

back2dos commented 6 years ago

Given this view:

class Foo extends View {
  @:state var bar = 1;
  function render() '
     <div>
       <Child onblub={bar++} />
       <span>{bar}</span>
     </div>
  ';
}

The blub event handler is a function that always does the same thing, but at every rerender a new function gets created, thus invalidating the view.

For anonymous functions, it should be possible to find out which variables they capture (in this case none) and then use those (and the string represenation of the function's AST) as a cache key. For example in TodoMVC the item and function (event) todos.delete(item) would be the cache key.

back2dos commented 4 years ago

For event handlers this is covered quite well, due to the fact that (unless they're nullable) even a new anonymous function won't cause rerendering.