Repository navigation
perf(js/ts): standard library fold numeric hash directly into accumulator instead of allocating intermediate - #5149
Open
shayanhabibi wants to merge 1 commit into
Open
shayanhabibi wants to merge 1 commit into
shayanhabibi wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Keep the captured array length and left-to-right element hashing, but fold each numeric hash directly
into the accumulator instead of allocating an intermediate
number[]and traversing it again.Experiments and results
Strict TypeScript compilation passed. The candidate passed 5,095 differential/behavior assertions; the combined candidate also passed 5,095. Allocation instrumentation confirmed the temporary hash-array constructor is eliminated (the unchanged implementation failed that gate). This does not quantify actual allocated bytes after JIT optimization.
Primary paired timing results from paired-array.json, 11 blocks per case:
Unchanged numeric control: +0.04% paired time. Earlier fresh-process rounds also favored the array change. These are warm microbenchmarks on Node v26.7.0, not application-level speedups.
Semantic Proofs
Maintaining F# semantics
The fold uses the same zero seed and
((h << 5) + h) ^ elementHash, retaining JavaScript's existing bitwise conversion/overflow behavior. Capturing length before traversal retains behavior when element getters/custom hashes resize the array. Reads and custom hash invocations remain in the same order, including sparse-array reads and exceptions. Only side-effect-free arithmetic on numeric hashes moves earlier.Coverage includes sparse arrays, numeric typed arrays including bigint, long arrays beyond .NET hashing sampling thresholds, nested composites, NaN/infinities/-0, mutation and throwing hash methods. Exact hashes match the JS baseline. Actual F# Dictionary/HashSet and collision coverage had the same 1,885 passes and two pre-existing nested-option failures as baseline. No new observed semantic differences; full conformance is not claimed.
Estimated blast radius
Implementation surface: one internal function in
Util.ts. Behavioral reach: broad within JS/TS structural hashing—arrays, typed arrays, tuples represented as arrays, collection keys containing arrays, and nested composite hashes. Equality, ordering, representation and .NET compiler paths are unchanged. Any hash regression could break collection lookup, so downstream risk is medium despite the small patch. Custom hashes must honor the numeric return contract; malformed JS coercible returns are not covered.