Skip to content

Conversation

riqts
Copy link

@riqts riqts commented Sep 20, 2025

Performance Optimization: isFrozen Fast Path

Problem

CPU profiling revealed that isFrozen() was consuming 46% of total CPU time in performance-critical scenarios, with 5.8M samples in benchmarks. The function was being called frequently on primitive
values (null, undefined, numbers, strings) which are conceptually "frozen" but require expensive Object.isFrozen() calls.

Solution

Added a fast path for primitives and null/undefined values that returns true immediately without calling Object.isFrozen().

export function isFrozen(obj: any): boolean {
    // Fast path: primitives and null/undefined are always "frozen"
    if (obj === null || typeof obj !== "object") return true

    return Object.isFrozen(obj)
}

Performance Impact

  • 47x reduction in isFrozen CPU usage (5.8M → 123K samples)
  • Overall v10 performance improved significantly
  • isFrozen CPU share dropped from 46% to 1% in benchmarks

Benchmarks

Before optimization:

  • v10 isFrozen: 5,823,243 samples (46% of total CPU)

After optimization:

  • v10 isFrozen: 123,246 samples (1% of total CPU)

Rationale

  • Primitives are immutable by nature, so treating them as "frozen" is semantically correct
  • typeof obj !== "object" check is orders of magnitude faster than Object.isFrozen()
  • No behavioral changes - maintains the same API and return values
  • Avoids complex caching strategies that proved to have too much overhead

Testing

  • Existing test suite passes
  • Performance benchmarks show significant improvement
  • No behavioral regressions detected
  • Covers edge cases (null, undefined, various primitive types)

Breaking Changes

None - this is a pure performance optimization with identical behavior. (I hope)

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.

1 participant