Measure performance and modify viewportMargin accordingly

So… I’m dealing with the infamous “People want to use browser search, but performance of large sets of text is terrible with viewportMargin = Infinity”-issue.

I’ve read quite a bit, and tried out several approaches. Now my latest idea was… What if… I measure the time it takes for CodeMirror to run an update and use that metric to dynamically adapt viewportMargin to a lower value, if the performance is ‘bad’.

But I can’t find an accurate way to measure that that doesn’t involve keeping track of each and every operation, and I was wondering if I didn’t happen to miss some sort of internal value that I could potentially abuse ? :smiley:

Because I think that would solve the problem. Optimise performance when people edit, but then as things stabilise, move it back to viewportMargin infinity to enable search.

My fallback idea is to just hook into keypress/up events and simply always adapt viewportMargin to a lower value, regardless of the performance, then have a delayed timer reset back to infinity if the last key press has been a while… Anyone tried something similar ?

So what I came up with was this. Sharing for others that are looking for solutions to this problem.

var activeViewportMargin = 10; // also initial value, until the initial update has completed
var restingViewportMargin = Infinity;

// We don't want to have to render too many lines when we are making changes
codeMirror.on( 'beforeChange', function ( cmInstance /* , change*/ ) {
	cmInstance.setOption( 'viewportMargin', activeViewportMargin );
} );
// But after a second of rest, draw everything, so that browser search will work.
codeMirror.on( 'update', OO.ui.debounce( function ( cmInstance ) {
	cmInstance.setOption( 'viewportMargin', restingViewportMargin );
}, 1000 ) );

Still interested in finding a performance based metric however.