wnr / element-resize-detector

Optimized cross-browser resize listener for elements.
MIT License
1.32k stars 118 forks source link

Memoize scrollbar width #125

Closed davidswinegar closed 4 years ago

davidswinegar commented 4 years ago

Store and reuse scrollbar width used in the scroll detection strategy, as generated by the getScrollbarSizes function, instead of generating it on every use of element-resize-detector.

For context, when profiling I noticed this change improves page load speed by ~3-10ms for every use.

wnr commented 4 years ago

Hi!

Yes, creating a new instance introduces some overhead. That's why most people make sure to only create one instance of the element-resize-detector and share it across the app (either by dependency injection or having a global instance). It's important to keep everything localized to an instance, instead of having global state. Otherwise testing and usage across frames would become harder.

I'm glad you created a PR, but I have to reject it. Please get back to me if you have any questions :)