-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use binary search in getVisibleElements() #5595
Use binary search in getVisibleElements() #5595
Conversation
cebc851
to
6a72760
Compare
Really like binary search idea :) I think you can perform binary search only once: to find first div that lays on (or about to touch) top viewport boundary. Then we can scan down to find bottom one (I think using binary search for second part might be slower since it will touch more of clientTop and other similar DOM properties) On different note: did you look into using https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect ? |
that might be true, but since the second binary search is very cheap (because it starts where the first search ends), it wouldn't matter much.
I don't think that matters too, because once the layout is done (after the first call of top, clientTop, or similar), further calls to these properties are quite fast.
I'll have a look. |
More numbers: |
to be fair, using the key down is not even realistic. this method was hot before i added the rAF debouncing. its still hot on mac which likes to fire millions of scroll events when using the touchpad. everything helps here. even 12µs. |
Since we have rAF now, the method is not be called too often. The method still seems to be very heavy as shown in the profiler, but it isn't: Most of the time is spent in the first line |
0ba69b0
to
351909a
Compare
Ok, after thinking about it I agree that linear search might even be faster for the second part, because binary search doesn't profit from the fact that the item to find is likely very close to the start of the search. |
@yurydelendik I had a look, but honestly I don't know how this could be helpful. |
351909a
to
347cdde
Compare
if ((currentHeight + viewHeight) < top) { | ||
continue; | ||
} | ||
elem = view.el; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We removed el
from the page/thumb. Could you replace this with div
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: elem -> element
8fe8820
to
f47dcb1
Compare
f47dcb1
to
a78bb6b
Compare
@yurydelendik I addressed your comments. |
/botio-linux preview |
From: Bot.io (Linux)ReceivedCommand cmd_preview from @timvandermeij received. Current queue size: 0 Live output at: http://107.21.233.14:8877/271bf1a4bf2caf0/output.txt |
From: Bot.io (Linux)SuccessFull output at http://107.21.233.14:8877/271bf1a4bf2caf0/output.txt Total script time: 1.00 mins Published |
/botio unittest |
From: Bot.io (Linux)ReceivedCommand cmd_unittest from @yurydelendik received. Current queue size: 0 Live output at: http://107.21.233.14:8877/e384c9cbfd80aa1/output.txt |
From: Bot.io (Linux)SuccessFull output at http://107.21.233.14:8877/e384c9cbfd80aa1/output.txt Total script time: 0.71 mins
|
From: Bot.io (Windows)ReceivedCommand cmd_unittest from @yurydelendik received. Current queue size: 0 Live output at: http://107.22.172.223:8877/f3c665227f34dc8/output.txt |
From: Bot.io (Windows)SuccessFull output at http://107.22.172.223:8877/f3c665227f34dc8/output.txt Total script time: 0.70 mins
|
…Elements Use binary search in getVisibleElements()
Really good. Thank you for the patch. |
The method
getVisibleElements()
uses a linear search to find the first page and thumbnail in the visible range. It is called on every scroll event, so it can take a noticeable amount of time for the bottom pages of a very large document.This PR replaces the linear search with a binary one (which needs O(log n) time).
A good test:
[3] pdf (5000+ pages)
A benchmark:


Scrolling through pages 5500 to 5515 of [3], using the
↓
key, Linux, Firefox Nightly 37.0a1,textLayer=off
Before:
After:
(Admittedly, the document has to be very large to notice a difference, but it should be more pronounced on slow devices).