Skip to content

perf: cache prototype chain walk to reduce construction from O(M*D) to O(M)#109

Merged
hectorhdzg merged 1 commit into
microsoft:mainfrom
hectorhdzg:perf/cache-proto-chain-walk
May 19, 2026
Merged

perf: cache prototype chain walk to reduce construction from O(M*D) to O(M)#109
hectorhdzg merged 1 commit into
microsoft:mainfrom
hectorhdzg:perf/cache-proto-chain-walk

Conversation

@hectorhdzg
Copy link
Copy Markdown
Member

_getBaseFuncs() previously re-walked the entire prototype chain on every instance creation, costing O(M*D) for M methods and D inheritance depth.

This adds _getBaseProtoFuncs() which caches the {funcName -> sourceProto} mapping on the class prototype (own property) after the first walk. All subsequent instance constructions reuse the cached mapping and only create per-instance proxy closures in O(M) time.

Key detail: uses objHasOwnProperty to check cache presence, preventing child classes from accidentally inheriting a parent's cache through the prototype chain.

Adds comprehensive test suite (DynamicProtoCache.Tests.ts) validating:

  • Multi-instance correctness with shared cache
  • Sibling classes with independent caches
  • Deep 3-level dynamic chains
  • Non-dynamic classes sandwiched between dynamic classes
  • Many-method resolution
  • Stress test with 50 rapid instances

…o O(M)

_getBaseFuncs() previously re-walked the entire prototype chain on every
instance creation, costing O(M*D) for M methods and D inheritance depth.

This adds _getBaseProtoFuncs() which caches the {funcName -> sourceProto}
mapping on the class prototype (own property) after the first walk. All
subsequent instance constructions reuse the cached mapping and only create
per-instance proxy closures in O(M) time.

Key detail: uses objHasOwnProperty to check cache presence, preventing
child classes from accidentally inheriting a parent's cache through the
prototype chain.

Adds comprehensive test suite (DynamicProtoCache.Tests.ts) validating:
- Multi-instance correctness with shared cache
- Sibling classes with independent caches
- Deep 3-level dynamic chains
- Non-dynamic classes sandwiched between dynamic classes
- Many-method resolution
- Stress test with 50 rapid instances
@hectorhdzg hectorhdzg merged commit f885e77 into microsoft:main May 19, 2026
3 checks passed
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.

3 participants