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

Improve VDOM's iteration performance #145

Open austinshenk opened 5 years ago

austinshenk commented 5 years ago

I went through and audited the for-loops and while-loops in VDOM's kernel code. I converted all of the forward iteration loops to use length - i instead of i < length and made sure the length value is being cached in a variable instead of being accessed each iteration. Example performance details are recorded in https://github.com/elm/core/pull/1000.

I plan to take the changes I made and compare some benchmarks to show the results. These changes should not be destructive to existing functionality since I've tested a number of functions already and have not found anything that compromises it.

drathier commented 5 years ago

The length property of strings is immutable, so caching it will only slightly reduce performance in modern browsers; it's already constant time to access. < vs - should also not make a noticeable difference, as code will be specialized to ints by the jit compiler. It's better to keep the code easy to read imo.