Skip to content

Performance: Cache getComputedStyle result in shadowCaretRangeFromPoint (mouse hot path) #319803

@ishaq2321

Description

@ishaq2321

Problem

In src/vs/editor/browser/controller/mouseTarget.ts:1136-1142, the shadowCaretRangeFromPoint function calls getComputedStyle(el, null) six separate times to retrieve individual CSS font properties (font-style, font-variant, font-weight, font-size, line-height, font-family) and then concatenates them into a font shorthand string.

Each getComputedStyle() call forces a synchronous style recalculation, which is expensive. Since this function runs on every mouse move over the editor viewport's shadow DOM content, the overhead is multiplied across every mousemove event in the editor.

Fix

Cache the getComputedStyle() result once in a local variable, then call .getPropertyValue() on the cached result:

- const fontStyle = elWindow.getComputedStyle(el, null).getPropertyValue('font-style');
- const fontVariant = elWindow.getComputedStyle(el, null).getPropertyValue('font-variant');
- const fontWeight = elWindow.getComputedStyle(el, null).getPropertyValue('font-weight');
- const fontSize = elWindow.getComputedStyle(el, null).getPropertyValue('font-size');
- const lineHeight = elWindow.getComputedStyle(el, null).getPropertyValue('line-height');
- const fontFamily = elWindow.getComputedStyle(el, null).getPropertyValue('font-family');
+ const computedStyle = elWindow.getComputedStyle(el, null);
+ const fontStyle = computedStyle.getPropertyValue('font-style');
+ const fontVariant = computedStyle.getPropertyValue('font-variant');
+ const fontWeight = computedStyle.getPropertyValue('font-weight');
+ const fontSize = computedStyle.getPropertyValue('font-size');
+ const lineHeight = computedStyle.getPropertyValue('line-height');
+ const fontFamily = computedStyle.getPropertyValue('font-family');

Impact

  • 6x reduction in forced style recalculations on every mouse move over editor content
  • No functional change — identical output, same behavior
  • Low-risk, one-line addition, single file change

File

src/vs/editor/browser/controller/mouseTarget.tsshadowCaretRangeFromPoint function

Related

This is a well-known browser performance best practice — MDN recommends caching getComputedStyle() results when reading multiple properties: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle#notes

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bugverifiedVerification succeeded

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions