nteract / nteract

📘 The interactive computing suite for you! ✨

Home Page:https://nteract.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Monaco perf improvement: Update the editor focus only when its prop value has changed

vivek1729 opened this issue · comments

To update editor's focus state, we check if the passed in prop is true and if the editor is not already focused:

// Set focus
if (editorFocused && !this.editor.hasTextFocus()) {
this.editor.focus();
}

Imagine a scenario where the passed in prop is true but the editor was already focused. We can see that we'd still end up calling the core hastTextFocus api which can lead to low level dom queries and potential layout thrashing.

This can be optimized by checking prevProps, following a similar pattern:

// Ensures that the source contents of the editor (value) is consistent with the state of the editor
// and the value has actually changed.
if (prevProps.value !== this.props.value && this.editor.getValue() !== this.props.value) {
this.editor.setValue(this.props.value);
}