elm / virtual-dom

The foundation of HTML and SVG in Elm.
https://package.elm-lang.org/packages/elm/virtual-dom/latest
BSD 3-Clause "New" or "Revised" License
209 stars 80 forks source link

Use Object.create(null) instead of {} to compute keyed diffs #180

Open jfmengels opened 2 years ago

jfmengels commented 2 years ago

This is a minor improvement pull request.

{} creates an empty object but which still contains methods and properties like toString. Checking changes[key] when the key is for instance toString creates false positives in that check.

I was looking whether into whether this caused bugs, but in practice, the algorithms in _VirtualDom_insertNode and _VirtualDom_removeNode end up with the correct behavior because they recurse with a modified key, but do so with an unnecessary iteration.

Object.create(null) creates an empty object without any methods or properties, leading to no false positives and one less unnecessary iteration.

mozfreddyb commented 2 years ago

Why not Map()?

jfmengels commented 2 years ago

Because Elm currently compiles down to ES5, where Map is not available.