TwicPics / components

A Web component library that brings the power of TwicPics to your favorite web framework.
MIT License
53 stars 2 forks source link

Resize events can be really harmful #33

Closed Elindorath closed 1 year ago

Elindorath commented 1 year ago

Hi there!

During the integration of your React component in a Next.js application, I noticed that the performance could be very degraded when resizing the window with the mouse.

For some context, I ran some tests on two pages in particular:

When trying to resize the browser window on these two pages, I observe different behavior when inspecting the network:

Based on these observations, I would tend to think we could benefit of the use of a timer to debounce unnecessary requests.

I tried to dig a bit into the source code to see how this could be done, and I thought I understood that something should be done when using the resizeObserver API in src/_/placeholder.ts but I'm having trouble seeing how to do it.

mbgspcii commented 1 year ago

Hi @Elindorath and thank you for this feedback.

The numerous requests observed here should not be due to the resizeObserver. The latter is only responsible for the "preview" image. It generates new requests only when the ratio or the positioning of the image has been modified during the resize, as it is the case in this example: TwicPics x Next - Art directions

2022-09-15_101655

When the ratio or the positioning mode of the image is not modified during the resize, the preview should never be reloaded.

On the other hand, the other requests (those responsible for the final image) are triggered when the difference (in absolute value) between the width of the display area of the image and its intrinsic width is greater than the step (10 px by default).

As a first step, I would propose to increase the value of the step. To increase it to 100 for example.

Globally :

2022-09-15_103857

or for a particular component

2022-09-15_151633

Here is a link to a Next example with many images and a step set to 100.

Note that I observe performance degradations if I change the browser size with the mouse for too long as the network console fills up. Closing the console or clearing the log "fixes" the problem.

Hoping that the increase in step value will help you.

Keep us informed.

Best regards.

mbgspcii commented 1 year ago

Hi @Elindorath.

For your information, changes concerning the resize have been made in version 0.11.0.

I take this release as an opportunity to ask you if you have noticed an improvement by increasing the step?

I close the issue but you can reopen it if you still have performance problems.

Keep us informed.

Best regards.