From 6bec772fd1e204b758c9ad27d596e53238888624 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 28 Oct 2020 16:48:37 +0000 Subject: [PATCH] Avoid binding events in duplicate This is particularly important as a resize event will trigger a call to this (via resize -> _init() -> set() -> image load -> image load -> bindEvents()) and the 'hover' event type also triggers a 'mouseover' event having added an event listener for the same. This all means that each time a browser sends a resize event (which can happen on other events than a viewport width/height change) more event listeners are stacked, leading to significant CPU usage for no value. --- lib/web/magnifier/magnifier.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/web/magnifier/magnifier.js b/lib/web/magnifier/magnifier.js index 155b946b68467..f6d97f40d51a5 100644 --- a/lib/web/magnifier/magnifier.js +++ b/lib/web/magnifier/magnifier.js @@ -401,6 +401,13 @@ } function bindEvents(eType, thumb) { + var eventFlag = 'hasBoundEvent_' + eType; + if (thumb[eventFlag]) { + // Events are already bound, no need to bind in duplicate + return; + } + thumb[eventFlag] = true; + switch (eType) { case 'hover': hoverEvents(thumb);