Set attributes in bulk

I’m measuring how long it takes the code below to set attributes to nodes in bulk:

  tr.doc.nodesBetween(0, tr.doc.content.size,
    (node, pos) => {
      tr.setNodeAttribute(pos, "foo", "bar")
      return false;

For 1000 nodes, it takes ~80ms, for 2000 nodes it takes ~300ms. However, by 4000 nodes it has jumped up to ~1200ms, which is longer than ideal. Is there a faster method of setting attributes in bulk?

You could write a custom Step subclass that does this in a single pass. (Or, if you don’t need a step, just write a function that converts your document recursively.) Using setNodeAttribute will create a separate step (and thus document update) for every node.

1 Like