# Why selectedCell decoration so slow in large document

**URL:** <https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175>\
**Category:** Uncategorized\
**Created:** [November 8, 2021, 7:39am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175 "2021-11-08T07:39:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 8, 2021, 7:39am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/1 "2021-11-08T07:39:17Z")

</div>

i use `tableEditing` in table node, its slow when i select the cell in large document|

 ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/8/8b1a426d4dea15eee90241c7364df277a841a1d1.png)this is in small document ![Jietu20211109-114302-HD](https://discuss.prosemirror.net/uploads/secondsite/original/2X/2/228e04fd9049a140f71fc1766e5f57de68dd6403.gif)

this is in large document

![Jietu20211109-114807-HD](https://discuss.prosemirror.net/uploads/secondsite/original/2X/8/88edb65196e4cc9a5e90d6088c2a2d43c991a92b.gif)

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 9, 2021, 10:32pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/2 "2021-11-09T22:32:04Z")

</div>

I noticed this as well and have a patch for this which speeds up compute time greatly for large table nodes. It essentially debounces a particular handler for column resizing which is triggered a lot when user interacts with table (mouse over)

I will try to open a pull request soon and maybe it will help you but let me know if you are using column resizing or not.

There might be limitations to my implementation so I am curious what the other authors think.

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 9, 2021, 11:20pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/3 "2021-11-09T23:20:26Z")

</div>

Again, this just targets the mousemove handler which fires too often for larger table nodes (if you used column resizing). If you want to copy this in to your node\_modules you can give it a try quickly.

> **[Comparing...](https://github.com/ProseMirror/prosemirror-tables/compare/master...ProjectAMPLE:performance-columnresizing-domevents?expand=1)**
>
> Table module for ProseMirror. Contribute to ProseMirror/prosemirror-tables development by creating an account on GitHub.

Edit: Opened a PR - [Improve performance of column resizing mousemouse handler by debouncing by bZichett · Pull Request #154 · ProseMirror/prosemirror-tables · GitHub](https://github.com/ProseMirror/prosemirror-tables/pull/154)

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 10, 2021, 3:00am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/4 "2021-11-10T03:00:37Z")

</div>

Yea, I used column resizing, but it’s still slow after the column resizing is disabled.

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 18, 2021, 11:34am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/5 "2021-11-18T11:34:17Z")

</div>

I’d love to help you out still 👍.

See if you can find where in the plugin most computation time is happening and we’ll see if we can work on some improvements. Let me know if you haven’t used a javascript profiler yet though as I don’t know your experience.

I started with column resizing only because it decimated performance with that one handler for large tables (but the PR is just reducing transaction on mouse events not table selections which require expensive ones that might be throttled as well in a similar manner).

There are likely a few more hot code paths that could be optimized beyond this as well.

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 19, 2021, 2:34am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/6 "2021-11-19T02:34:19Z")

</div>

I found the reason, when the `tableEditing` create the selectedCell decorations,it will enter to `iterDeco` function. the iterDeco function speed up compute time greatly. when it forChild, the condition `result == empty` is not true, even though the result is empty, but they are not the same variable, which will cause foreachall nodes of document(large document will speed up compute time greatly)

```auto
 DecorationGroup.prototype.forChild = function forChild (offset, child) {
  if (child.isLeaf) { return DecorationSet.empty }
  var found = [];
  for (var i = 0; i < this.members.length; i++) {
    var result = this.members[i].forChild(offset, child);
    if (result == empty) { continue }
    if (result instanceof DecorationGroup) { found = found.concat(result.members); }
    else { found.push(result); }
  }
  return DecorationGroup.from(found)
}

```

So, I copy the tableEditing code from `prosemirror-tables` into my project. then it’s solved. I’m not sure this is a bug of `prosemirror-view`?

the following is a part of tableEditing code

```auto
export function tableEditing({ allowTableNodeSelection = false } = {}) {
  return new Plugin({
    key: tableEditingKey,
    state: {
      init() {
        return null;
      },
      apply(tr, cur) {
        let set = tr.getMeta(tableEditingKey);
        if (set != null) return set == -1 ? null : set;
        if (cur == null || !tr.docChanged) return cur;
        let { deleted, pos } = tr.mapping.mapResult(cur);
        return deleted ? null : pos;
      },
    },

    props: {
      decorations: drawCellSelection,

      handleDOMEvents: {
        mousedown: handleMouseDown,
      },

      createSelectionBetween(view) {
        if (tableEditingKey.getState(view.state) != null)
          return view.state.selection;
      },

      handleTripleClick,

      handleKeyDown,

      handlePaste,
    },

    appendTransaction(_, oldState, state) {
      return normalizeSelection(
        state,
        fixTables(state, oldState),
        allowTableNodeSelection,
      );
    },
  });
}

export function drawCellSelection(state) {
  if (!(state.selection instanceof CellSelection)) return null;
  let cells = [];
  state.selection.forEachCell((node, pos) => {
    cells.push(
      Decoration.node(pos, pos + node.nodeSize, { class: 'selectedCell' }),
    );
  });
  return DecorationSet.create(state.doc, cells);
}

```

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 19, 2021, 1:52pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/7 "2021-11-19T13:52:44Z")

</div>

Ah I see. There is another handler that cannot keep up with large tables atm so for the time being, I’d like to test this approach which I just finished up now. It throttles the mousemove during cell selection changes. Again a little hit to the UX but required at the moment for a functional user experience.

> <https://github.com/ProseMirror/prosemirror-tables/pull/157>

Copy over the two core files to test it out (only two files that changed that you need to swap into node\_modules, the rest are tests and demo updates)

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 19, 2021, 2:20pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/8 "2021-11-19T14:20:52Z")

</div>

Testing a public exposed npm module here: @projectample/prosemirror-tables

If you are using webpack, alias works well

```auto
  'prosemirror-tables': '@projectample/prosemirror-tables',

```

Its better but still not quite there. Still investigating but feel free to try that out. Seeming more like a memory leak in `tableEditing`, since the first interaction for dragging works well but then the next one slows down. This also happens when there are multiple tables in the same document of medium / small size (dozens)

How do I know this: I disabled **all** the decorations and plugins of the EditorView (except `tableEditing`) and the performance was OK but only for the first few interactions (anything, including drags) but then it still degraded completely to the same as before. So its likely not caused by decorations but exacerbated severely if there are a lot of them.

@jiaxiaxia Check a browser different than Chrome (or let me know what browser you are using)

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 19, 2021, 4:27pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/9 "2021-11-19T16:27:36Z")

</div>

Might be related to `DOMObserver` usage. It appears that Chromes javascript profiler is obfuscating whatever is long running.

Ok, finally reporting something interesting; the text in the table I was working with had spelling corrections pending, and it appears Google chromes implementation has to refire on every interaction.

I am aware of some threads about spellcheck but I was not aware of the performance ramifications being this deep. Is is possible your table is filled with non-English characters that your browser is attempting to correct as well?

> [@Disable spellcheck - add attribute to contenteditable div](https://discuss.prosemirror.net/t/disable-spellcheck-add-attribute-to-contenteditable-div/3038):
>
> Hey folks, I was looking for the best path to add an attribute to the contenteditable div that is setup by prosemirror. On Chrome, spellchecking is being automatically applied, which I would like to disable by adding a spellcheck='false' attribute. [image] What’s the easiest path forward to accomplish this? Thanks, James

Performance stabilized by adding the spellcheck=false attribute to just the

 element.

@marijn Thoughts on this? I reached out to a prosemirror-table author and he said he wasnt working on it anymore; Been trying to improve performance there (2 open PR’s) but this spell check one might be heavily effecting non english Chrome browser users more than anything. Im not sure if its related to `prosemirror-tables` and its `DOMObserver` usage or a core prosemirror library

Edit: I see this comment now - [What is the current state of spellcheck? - #11 by marijn](https://discuss.prosemirror.net/t/what-is-the-current-state-of-spellcheck/456/11)

> <https://github.com/ProseMirror/prosemirror-view/blob/94a7758e0834be4624464dc506f27bb751224138/src/input.js#L37>

---

<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 19, 2021, 6:42pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/10 "2021-11-19T18:42:45Z")

</div>

> [@bZichett](#):
>
> @marijn Thoughts on this?

I am not planning to maintain prosemirror-tables either. It’d be great if a new maintainer can be found, but it’s not going to be me.

I’m not sure what you mean by DOMObserver usage and how that is related to this. Turning spell-checking off is an option sites have, but not something I want to make the library do by default.

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 19, 2021, 10:03pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/11 "2021-11-19T22:03:59Z")

</div>

Well, one thing I know for sure is that a large table filled with German language that triggers chromes spell check causes massive performance issues. Its exacerbated by tables due to the more complex DOM structure, presuming.

There is definitely more room for figuring out some performance issues with prosemirror-tables though. I’ll be looking into this more.

As for the spellcheck attribute. I added a conditional attribute prop for when a large doc is received by editor state (sufficiently large \> 10,000 to start.) Its fine not to have built in to prosemirror.

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 20, 2021, 3:07pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/12 "2021-11-20T15:07:50Z")

</div>

> [@bZichett](#):
>
> document

I used the chrome 95.0.4638.69,I didn’t used webpack, I used vite

---

<div class="post-metadata">

**Author:** ![bZichett](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bzichett/32/1047_2.png) [@bZichett](https://discuss.prosemirror.net/u/bZichett)\
**Post date:** [November 21, 2021, 4:45pm UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/13 "2021-11-21T16:45:22Z")

</div>

@jiaxiaxia Does your document have a lot of text that is triggering chromes auto spell checker? (Or just an empty table cells?)

If it’s empty table cells, the debounce and throttle in the pull requests I have for prosemirror-tables should help a lot.

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 22, 2021, 7:07am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/15 "2021-11-22T07:07:51Z")

</div>

@marijn is it a bug of prosemirror-view ,or it’s just a result of package tools

 ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/0/010c4c278e027d189aa5b611418e020086fb9f7e.png)

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 22, 2021, 7:17am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/16 "2021-11-22T07:17:50Z")

</div>

Yea，it did help a lot, but it is not the main reason for me. my main proplem is [Why selectedCell decoration so slow in large document - #5 by bZichett](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/5)

---

<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 22, 2021, 7:26am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/17 "2021-11-22T07:26:00Z")

</div>

> [@jiaxiaxia](#):
>
> @marijn is it a bug of prosemirror-view ,or it’s just a result of package tools

I don’t know—I couldn’t make much of the `iterDeco` comments you made.

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 22, 2021, 8:01am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/18 "2021-11-22T08:01:58Z")

</div>

> [@marijn](#):
>
> make much

I’m sorry，the following is the iterDeco function

 ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/2/2e5f7e1e99caf4b45d06056a9c492a34bb75782c.png) ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/1/1cc501ec87361b3c717a09e0f3ba99d8e7fbc8e1.png)

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 22, 2021, 8:14am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/19 "2021-11-22T08:14:54Z")

</div>

Again, spellcheck has been diabled

 ![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/5/523b6863c373ad8408f961a6931997f3b51abdc0.png)

---

<div class="post-metadata">

**Author:** ![jiaxiaxia](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/jiaxiaxia/32/1976_2.png) [@jiaxiaxia](https://discuss.prosemirror.net/u/jiaxiaxia)\
**Post date:** [November 22, 2021, 8:37am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/20 "2021-11-22T08:37:28Z")

</div>

the third parameter of the `onNode` function will call deco.forChild. when forChild, the `result` is empty$4, but the empty is empty$1 after build ,so they are different.

---

<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 22, 2021, 9:56am UTC](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175/21 "2021-11-22T09:56:35Z")

</div>

Could it be that you’re loading multiple versions of prosemirror-view?

[Next page](https://discuss.prosemirror.net/t/why-selectedcell-decoration-so-slow-in-large-document/4175.md?page=2)
