Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 7, 2026

Cache horizontal spacing calculations to reduce getComputedStyle() calls from O(n) to O(1) in the ActionList layout method.

Changes

  • Cache computed styles: Calculate padding/margin once for the first element instead of per-item
  • Reuse cached value: All items share the same CSS classes, making this safe

Implementation

// Cache computed style values since they're consistent across all list items
let cachedHorizontalSpacing: number | undefined;

const itemWidths: number[] = this._allMenuItems.map((_, index): number => {
    const element = this.domNode.ownerDocument.getElementById(this._list.getElementID(index));
    if (element) {
        // ... measure content width ...
        
        // Only compute style values once for the first element, then reuse for all others
        if (cachedHorizontalSpacing === undefined) {
            const computedStyle = dom.getWindow(element).getComputedStyle(element);
            const paddingLeft = parseFloat(computedStyle.paddingLeft) || 0;
            const paddingRight = parseFloat(computedStyle.paddingRight) || 0;
            const marginLeft = parseFloat(computedStyle.marginLeft) || 0;
            const marginRight = parseFloat(computedStyle.marginRight) || 0;
            cachedHorizontalSpacing = paddingLeft + paddingRight + marginLeft + marginRight;
        }
        
        return width + cachedHorizontalSpacing;
    }
    return 0;
});

Addresses performance feedback on parent PR #286343.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Jan 7, 2026
Copilot AI changed the title [WIP] Address feedback on width calculation improvements in ActionList Optimize getComputedStyle calls in ActionList layout method Jan 7, 2026
Copilot AI requested a review from mrleemurray January 7, 2026 12:03
@mrleemurray mrleemurray marked this pull request as ready for review January 7, 2026 12:16
@mrleemurray mrleemurray merged commit 6237fbc into mrleemurray/intermediate-guineafowl-sapphire Jan 7, 2026
5 of 6 checks passed
@mrleemurray mrleemurray deleted the copilot/sub-pr-286343 branch January 7, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants