Font-Awesome icon indicating copy to clipboard operation
Font-Awesome copied to clipboard

MUTATION_OBSERVER poor performance (observe method)

Open rafalpospiech opened this issue 6 years ago • 12 comments

There is a huge performance drop while MUTATION_OBSERVER is enabled. I have component on my page that uses SVG to display some content. This content is reactive and works on vue. There are a lot of mutations going on so MUTATION_OBSERVER is running like crazy all the time. I don't want to disable observer globally. Is there a way to exclude from observation some parts of the page? by class or id?

I'm using Font Awesome Free 5.5.0

chrome_2018-12-06_13-11-24 observer

rafalpospiech avatar Dec 06 '18 12:12 rafalpospiech

Hi!

Thanks for being part of the Font Awesome Community.

@robmadole could you please take a look at this?

tagliala avatar Dec 06 '18 12:12 tagliala

Mutation observer is attached to my <g> element inside svg component. This component does not use font-awesome icons at all.

rafalpospiech avatar Dec 06 '18 12:12 rafalpospiech

@rafalpospiech the MutationObserver by it's very nature needs to examine changes to the DOM to see if there is are any changes that it needs to generate icons for. There is currently no way to tell it to ignore certain parts of the page. You can control where the mutation observers starts, though. I don't know if this will help you or not.

You mentioned that you are using Vue. Can you switch to our Vue component?

robmadole avatar Dec 06 '18 22:12 robmadole

No, Vue is used just by this one component :/

rafalpospiech avatar Dec 07 '18 07:12 rafalpospiech

But take a look at this example (line 23)

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#Example

You can observe specific nodes only without subtree and with childnodes only.

So theoretically we can get all elements on the page and observe only those that doesn't have an attribute let say data-fa-no-observer or something like that with multiple observers I think.

If observer will be on each element there could be an option to specify which elements to observe and which exclude from observation. For example

{
/* ... */
observer:{
  include:[],
  exclude:[]
}
/* ... */
}

Or by default observe all excluding those with data-fa-no-observer. This would improve the performance a lot.

rafalpospiech avatar Dec 07 '18 07:12 rafalpospiech

@rafalpospiech when brand new elements are added to the page how would we detect those?

And with regard to your include and exclude options. Those elements would still need to be observed are you thinking that we could just bail out of processing them sooner?

robmadole avatar Dec 12 '18 17:12 robmadole

I know this is an old thread, and I don't have a solution to offer. Just adding an example I have been observing. XHR call is adding a rather long dropdown menu, prior to using the fontawesome.js file this took roughly 1-2 seconds to render upon call completion. When using frontawesome.js (5.2 in this case) rendering takes about 15 seconds. Screen Shot 2020-08-26 at 10 24 26 AM

dguettler avatar Aug 26 '20 14:08 dguettler

Same issue with angular 15 and fontawesome 6

While the icons are used in a static part of the page, the main portion (which is a calendar) renders many, many many dom elements. Adding fontawesome drops performance from 3s for a complex year view to an entire minute and a half!

DibyodyutiMondal avatar May 01 '23 17:05 DibyodyutiMondal

I'm having this problem as well in a SvelteKit application that builds a page that contains hundreds of icons. The FontAwesome mutation observer blocked the UI thread for around 8s on page load.

I resolved it by using FontAwesome in pure CSS mode (CSS + WebFont, no JS).

dbrgn avatar Dec 30 '23 22:12 dbrgn

@dbrgn you can also take a look at https://github.com/FortAwesome/svelte-fontawesome

robmadole avatar Jan 28 '24 18:01 robmadole

@robmadole I tried, but https://github.com/FortAwesome/svelte-fontawesome/issues/7

In the end, I'm actually happier with a pure-CSS mode, I don't need any of the dynamic features 🙂

dbrgn avatar Jan 28 '24 21:01 dbrgn