# Decorations performance

**URL:** https://discuss.prosemirror.net/t/decorations-performance/2381
**Category:** Uncategorized
**Created:** [November 29, 2019, 2:47am UTC](https://discuss.prosemirror.net/t/decorations-performance/2381 "2019-11-29T02:47:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jiewuza](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiewuza/32/1113_2.png) [@jiewuza](https://discuss.prosemirror.net/u/jiewuza)
#### Post date: [November 29, 2019, 2:47am UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/1 "2019-11-29T02:47:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ericksonc](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ericksonc/32/1242_2.png) [@ericksonc](https://discuss.prosemirror.net/u/ericksonc)
#### Post date: [November 29, 2019, 6:28am UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/2 "2019-11-29T06:28:48Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![jiewuza](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiewuza/32/1113_2.png) [@jiewuza](https://discuss.prosemirror.net/u/jiewuza)
#### Post date: [November 29, 2019, 11:40am UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/3 "2019-11-29T11:40:36Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ericksonc](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ericksonc/32/1242_2.png) [@ericksonc](https://discuss.prosemirror.net/u/ericksonc)
#### Post date: [November 29, 2019, 12:11pm UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/4 "2019-11-29T12:11:52Z")

</div>

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](https://discuss.prosemirror.net/t/apply-decorations-directly-through-an-editor-method/2377/7) - not sure if that’s helpful or not.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [November 29, 2019, 12:40pm UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/5 "2019-11-29T12:40:26Z")

</div>

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`).

---

<div class="post-metadata">

### Author: ![jiewuza](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiewuza/32/1113_2.png) [@jiewuza](https://discuss.prosemirror.net/u/jiewuza)
#### Post date: [November 30, 2019, 12:30pm UTC](https://discuss.prosemirror.net/t/decorations-performance/2381/6 "2019-11-30T12:30:48Z")

</div>

Thank you @ericksonc and @marijn

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