Why rendering is slow in GenericRenderWindow if we re-use it

In my application I did two tests to prouve what I’m saying:

1 case:
In each time I want to render a new 3D surface actor, I delete the whole vtkGenericRenderWindow and the previous actor / mapper, and I re-create a new ones, doing this the rendering is good but I’m risking to have Too many open WebGL contexts warning.

2 case:
In each time I want to render a new 3D surface actor, I re-use the same vtkGenericRenderWindow and I delete the previous actor/ mapper and I create new actor/ mapper, doing this the rendering is slow, so when I move the 3D surface with the mouse it is moving so slow.

The second case shouldn’t necessarily be slow, unless your data is very, very large. You typically never want to do #1.

Thank you for your answer,
Yes I can say that my data is very large, each polydata (used by the 3D surface actors) size is 30MB and I have 33 patients,

[video-to-gif output image]

This how I’m doing it in my code:

PS: context.current[3].GLWindow is the reference holding my GenericRenderWindow

const brainRender = async () => {
    
    if (context.current[3]) {
        const { actor, mapper, } = context.current[3];
          
        context.current[3].GLWindow.getRenderer().removeAllActors();
  
        if(actor){
          actor.delete()
        }
        if(mapper){
          mapper.delete()
        }
      }
    
    let pd, GLWindow, actor, mapper;
    mapper = vtkMapper.newInstance();
    actor = vtkActor.newInstance();
    GLWindow = context.current[3].GLWindow;

    const reader1 = vtkPolyDataReader.newInstance();
    await reader1
    .setUrl(path)
    .then(async () => {
        pd = reader1.getOutputData();
    })
    .catch((error) => {
        console.log(error);
    });

    const originBrain = origin.current;

    actor.setMapper(mapper);
    actor.setPosition(originBrain[0], originBrain[1], originBrain[2]);

    mapper.setInputData(pd);
    GLWindow.getRenderer().addActor(actor);
    GLWindow.getRenderer().resetCamera();
    GLWindow.getRenderer().getActiveCamera().zoom(1.3);
    GLWindow.getRenderer().getActiveCamera().pitch(2.3);
    GLWindow.getRenderWindow().render();

    const obj = {
      actor: actor,
      mapper: mapper,
      GLWindow: GLWindow,
      pd: pd,
    };

    context.current[3] = { ...obj };
  };

Doing it like this and after displaying 3 or 4 patients, manipulating the 3D surface with the mouse becomes so slow:

[video-to-gif output image]

Can you make a devtools performance profile of the slow 3D surface manipulation? I don’t suppose that you have all 33 polydatas visible in the scene, since it just looks like one. But it shouldn’t be that slow.

Thank you for your answer, I found the problem,

In each time I’m calling my onClick event I initialize the GLWindow variable with new vtkGenericRenderWindow

let GLWindow =  vtkGenericRenderWindow.newInstance();
if (context.current){
GLWindow = context.current[3].GLWindow
}
//...

So I just removed the initialisation and it works fine!

let GLWindow;
if (context.current){
GLWindow = context.current[3].GLWindow
}
//...