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

Allow for objects to be passed to lazyDefine, avoiding repeat calls #321

Conversation

keithamus
Copy link
Member

In our mob pairing session called "Making Dotcom Fast" we ran into an issue where we're calling lazyDefine over 200 times in quick succession. This becomes quite expensive as cancelAnimationFrame is O(n) and we're calling it 200 times meaning we end up with thousands of iterations that ultimately do nothing.

In order to prevent this cost on page load, we can overload lazyDefine to take an object of element->callback pairs, meaning we only call observe() once. This then needs a tiny refactor on the consumer side, but it turns this:

image

into this:

image

@keithamus keithamus requested a review from a team as a code owner October 23, 2024 13:31
Co-authored-by: AdamShwert <[email protected]>
Co-authored-by: Arelia Jones <[email protected]>
Co-authored-by: Chris Gavin <[email protected]>
Co-authored-by: Cody Bodfield <[email protected]>
Co-authored-by: Dusty Greif <[email protected]>
Co-authored-by: Elena Tanasoiu <[email protected]>
Co-authored-by: Erinna Chen <[email protected]>
Co-authored-by: Francesco Manni <[email protected]>
Co-authored-by: Jibran Garcia <[email protected]>
Co-authored-by: Mamadou Coulibaly <[email protected]>
Co-authored-by: Ned Schwartz <[email protected]>
@keithamus keithamus force-pushed the allow-for-objects-to-be-passed-to-lazydefine-avoiding-repeat-calls branch from 623860d to bd69f5d Compare October 23, 2024 13:32
Copy link
Contributor

@koddsson koddsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! <3

src/lazy-define.ts Outdated Show resolved Hide resolved
Co-authored-by: Kristján Oddsson <[email protected]>
@keithamus keithamus force-pushed the allow-for-objects-to-be-passed-to-lazydefine-avoiding-repeat-calls branch from 8cc9495 to 3a8e33a Compare October 23, 2024 14:04
@keithamus keithamus merged commit 5d9b9cb into main Oct 23, 2024
3 of 5 checks passed
@keithamus keithamus deleted the allow-for-objects-to-be-passed-to-lazydefine-avoiding-repeat-calls branch October 23, 2024 17:23
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

Successfully merging this pull request may close these issues.

2 participants