Optimizing restyle for multiple traces

I am working with a rather large dataset, 300K points in 5 subplots each, plotted with Scattergl. They are organized into various groups, and I need to be able to select an entire group in all subplots through varying the opacity. Here is the code I’m using to do it:

gd.on(‘plotly_click’, function (eventdata){
var curveNumber = eventdata.points[0].curveNumber;
for(i = 0; i<type1.length; i++){op[i]=( type1[i]==type1[curveNumber]? 1:0.03)};
Plotly.restyle(gd, ‘opacity’, op);

The problem is, with ~1500 individual groups, that is about 7500 individual traces to operate on, it takes about 8 seconds to update. Dividing them into smaller number of groups (about 1500 traces in total) but still with the same number of points overall can be executed in 1.5 seconds, which is acceptable, but at a loss of functionality.

Is there a way to better optimize the performance with many traces? Alternatively, is there a way to preserve selection of defined groups in which multiple groups share the same trace (I tried implementing something like this myself, but it kept crashing, so I wasn’t able to figure it out)?