Skip to content
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

[Performance] Reduce calls to getBoundingClientRect() #5

Open
devrelm opened this issue Jun 14, 2016 · 3 comments
Open

[Performance] Reduce calls to getBoundingClientRect() #5

devrelm opened this issue Jun 14, 2016 · 3 comments

Comments

@devrelm
Copy link
Owner

devrelm commented Jun 14, 2016

In some browsers, getBoundingClientRect() forces a new layout calculation on each call.

Currently, the resize-observer module calls getBoundingClientRect() multiple times during each animation frame, especially when the size has changed. We should instead cache this value the first time that it is called per animation frame and reuse that value.

@gajus
Copy link

gajus commented Sep 7, 2016

A quick and obvious win is to at least group capturing of width and height.

https://github.com/pelotoncycle/resize-observer/blob/master/resize-observer.js#L131-L132

This causes two layout calculations.

@gajus
Copy link

gajus commented Sep 7, 2016

You could further reduce the number of getBoundingClientRect calls by using MutationObserver: setup an observer on all parent elements. In combination with window "resize" event, this will give an indication when dimensions of the element could have changed. This approach is not bulletproof (e.g. CSS transitions), but it would cover 99% of use cases.

@que-etc
Copy link

que-etc commented Sep 13, 2016

Hi! I'd like to mention that it'd better to avoid gbcr at all as its data is affected by paddings, borders, scrollbars and CSS transformations, while width and height properties of a content rectangle represent content width and content height respectively. And while using it you need to check whether an element being observed is not an inline element.

Probably, the best thing to do would be to use either computed styles of width and height or clientWidth and clientHeight properties of an element. But the first approach gives incorrect results for an element whose box model is the border-box. And with client width/height you would only need to subtract paddings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants