The mouse movement event is inherently slow, since it fires with every change in the pixel.
The only thing you can do to optimize the whole problem is to optimize what you are doing inside the callback handler. In your case you have
if (something) doA(); else doB();
This means that in any case, you are trying to show or hide the scroll bar, even if it is already shown or hidden. What you can do is:
if (scrollBarIsVisible && x>20 && x<200) { hideScrollBar(); scrollBarIsVisible = false; } else if (!scrollBarIsVisible) { showScrollBar(); scrollBarIsVisible = true; }
So that you only change the visibility of an element (which can be a difficult operation, as events may need to be transmitted) when switching from borders to external and vice versa. This should significantly reduce computing operations.
source share