Skip to content

Skip redundant toProxy() calls in walkSync by caching proxy across enter/exit phases #67

@alanzabihi

Description

@alanzabihi

Hypothesis

In walkSync (lazy-result.js:525-574), node.toProxy() may be called up to 4 times for the same node: enter listeners (line 543), keyed enter (550), exit listeners (563), keyed exit (570). While toProxy() caches the Proxy on the node, each call still involves: function call overhead, WeakMap lookup (proxyCache), and conditional check.

The code already partially optimizes this with the local proxy variable, but it still calls toProxy() at each phase if the previous phase had no visitors. Cache the proxy unconditionally at the start of walkSync for any node that has ANY listeners:

// Pre-check: does this node type have any listeners at all?
let hasAnyVisitor = listeners[type] || listeners[type + 'Exit']
if (key) hasAnyVisitor = hasAnyVisitor || listeners[type + '-' + key] || listeners[type + 'Exit-' + key]
let proxy = hasAnyVisitor ? node.toProxy() : null

This reduces toProxy() calls from up to 4 per node to exactly 0 or 1.

Editable surface

  • lib/lazy-result.js — restructure walkSync to call toProxy at most once per node

What's different from prior work

Expected impact

METRIC_B improvement of 1-3ms. Combined with other optimizations in the same thesis, could contribute to crossing tolerance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions