Decorations performance

I was testing plugins with decorations. In my plugin, the decorations have to check many link nodes in the editor and update when the content of link nodes changes.

But I found that decorations were called whenever the view changed (e.g. cursor moved, selection changed, etc.), which could result in a lot of unnecessary work (as the link nodes do not change at all). And I am also wondering about the performance.

How about putting logic in the “apply” method of your Plugin that has logic to see if there’s anything in the transaction that your plugin needs to care about, before doing any iteration?

If I understand correctly, all plugins’ “apply” methods get called whenever a transaction is made, and it’s up to the plugins to decide what they do about it (if anything)

Yes, it makes sense. What I want to implement is that whenever the content of some link node is modified, this plugin will automatically update itself to be consistent.

But I don’t know how to check if the transaction is modifying the content of some link node and get the link node in the “apply” method. Would you please give more instructions?

I’m still pretty new to Prosemirror, and I’m not that clear on what you’re trying to do, so I’m not sure how much more help I can be.

I did recently do some work with applying decorations here - not sure if that’s helpful or not.

The general idea is that you keep your decorations in a plugin state field, and have your decorations method just read it from there, rather than rebuild it on every transaction. The field’s apply method maps the old decoration set when there are any changes, and updates the regions touched by the transaction’s steps (which you can get by iterating over their step maps, as found in tr.mapping).

1 Like

Thank you @ericksonc and @marijn

I will follow your instructions, and give it a shot.