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

Avoid DOM operation if applying an empty className #605

Closed
wants to merge 1 commit into from

Conversation

stanleyxu2005
Copy link

@stanleyxu2005 stanleyxu2005 commented Mar 17, 2024

Short version:

I noticed after image is loaded, vanilla-lazyload will automatically add a class loaded.

In my case is not needed (see the details in Long version section below). I'd like to avoid calling addClass and removeClass for a style not defined. It might not have any performance difference (my case I have 200 images in page and viewport can show 30 images) but who knows. The simple PR is about to check and avoid unnecessary DOM operation.

Long version:

I see vanilla-lazyload is recommended in web.dev when I was troubleshooting why native image lazy loading is not working even in modern browsers (issue 4273). My case works in Chrome 109, but does not work in newer browsers (e.g. Chrome 120-123, Edge 122). web.dev explains that developer needs to specify width and height, and native lazy loading might behave differently or even not working in the you expect.

I think I have to use a JS based solution to fix this either implementing IntersectionObserver myself or using vanilla-lazyload. I choose the second and that's why I share this feedback.

I'm using Vue and I have already a wrapper component <LazyImage> it wraps img element like this

<img :src=imageSrc loading="lazy" :class="{ease: true, easeLoaded: loaded}" @load="loaded=true">

By using vanilla-lazyload, I want to keep changes minimal. so I only rename src to datasrc, and remain the rest unchanged.

And then I initialize the lozyload like this

document.lazyLoadInstance = new LazyLoad({
  elements_selector: '[loading=lazy]', // no need to add class "lazy" to img
  class_applied: "",
  class_loading: "",
  class_loaded: "",
  // class_error: "error", // keep it
  class_entered: "",
  class_exited: "",
))

As you can see, if class_* is empty, it'll be great to skip any DOM operation.

wdyt?

Thanks

When image is loaded its onload event will be triggered and I can add my own "loaded" classname (e.g. ease-loaded). 

Now I use lazyload as a complementary solution for native lazy loading approach. I want to keep the adaption minimal, so I only replace `src` with `datasrc`, and then make sure `lazyLoadInstance.update()` is called when it is required. 

I noticed after image is loaded, `vanilla-lazyload` will automatically add a class `loaded`, which in my case is not needed.

I can set the class name `class_loaded=''` in it will still call `addClass` and `removeClass` once the loading state is changed. I want to optimize a tiny bit. 

It might not have any performance difference but who know.
@verlok
Copy link
Owner

verlok commented Mar 17, 2024

Thanks for the PR, I completely agree with you and find the purpose very useful for performance improvement.

@verlok
Copy link
Owner

verlok commented Mar 17, 2024

I won't merge it to master because the release process on NPM goes through different steps, I use GitFlow. Can you make a PR towards a new branch named, e.g. feature/avoid-class-add-remove-if-not-necessary?

If you can't, I will do it (but then your name won't show up in the contributors of this library).

@stanleyxu2005
Copy link
Author

Sure. New PR is created #606
I'm not sure how to add test case for that. so it's just change the source and dist files

@inglesuniversal
Copy link

That is in deed a very good improvement. Thank you both for your awesome discoveries!

@verlok
Copy link
Owner

verlok commented Mar 19, 2024

While I was at it, I took the chance to clean up the code from Internet Explorer specific checks, and released version 18.0.0 which is smaller, but it doesn't support IE11 anymore.

@verlok
Copy link
Owner

verlok commented Mar 19, 2024

If you’re happy with my support, the documentation and the effort I’ve been putting on this project in the latest years, I hope you might want to buy me a coffee to show your appreciation. Or sponsor me, if you're a fan.

Open-source software is great for everyone, but it takes passion and time (and coffee!) to grow and evolve.

Thank you for thinking about it.
Have a wonderful day!

@stanleyxu2005 stanleyxu2005 deleted the patch-1 branch March 27, 2024 11:43
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.

None yet

3 participants