Memoize scanPrototypesFor to avoid per-instance prototype walks#30
Merged
Conversation
scanPrototypesFor is called four times in KompElement's constructor and twice in TableColumn's, climbing the prototype chain each time to merge static class metadata (assignableAttributes, bindMethods, assignableMethods, events). The result depends only on the (constructor, key) pair, not the instance, so it's the same answer for every cell, row, and column built from the same class. Cache results in a WeakMap keyed by the constructor. The cache holds references to the per-prototype values, so plugin in-place mutations like `this.events.push(...)` remain visible after a class is first scanned. All existing call sites use `.filter(...).reverse().forEach(...)`, which copies before any reordering, so the cached arrays are never mutated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
d56ebd1 to
041c452
Compare
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
scanPrototypesFor(this.constructor, key)runs four times inKompElement's constructor (bindMethods,assignableAttributes,assignableMethods,events) and twice inTableColumn's — every cell, row, and column construction climbs the prototype chain and rebuilds the same arrays.WeakMapkeyed by the constructor →Mapkeyed by property name → result array. The cache stores references to the per-prototype values (not copies), so plugin in-place mutations likethis.events.push('columnResize', 'rowResize')inresizable.jsremain visible whether they run before or after first scan.Why this is safe
All current call sites use the pattern
scanPrototypesFor(...).filter(x => x).reverse().forEach(...).filterreturns a fresh array, and the subsequentreversemutates that fresh array — the cached array is never touched. The doc comment notes that callers should treat the returned array as read-only.Performance impact
Hottest path: building 1000×10 = 10,000 cells previously did 40,000 prototype walks (4 per cell). After this change it's 4 per Cell class total, then O(1) lookups thereafter.
Test plan
Spreadsheet.eventsincludes plugin-added entries (columnResize,rowResize, etc.) and thatonColumnResizeconstructor option still binds.npm run docs).🤖 Generated with Claude Code