francoischalifour / medium-zoom

🔎🖼 A JavaScript library for zooming images like Medium
https://medium-zoom.francoischalifour.com
MIT License
3.58k stars 160 forks source link

perf: subscribe to the browser event only when attaching images #217

Open negezor opened 5 days ago

negezor commented 5 days ago

Summary

Currently, we subscribe to the browser events click, keyup, scroll, and resize every time the mediumZoom function is called, which also happens during clone. Instead of always being subscribed to these events, it is much better to subscribe only when necessary. These events stack up on each other, significantly affecting performance and preventing the browser from garbage collecting. This is especially important in SPA applications when we use a component-based approach.

Result

No performance degradation when images are missing or not active.