Skip to content

perf: fast-path primitives in deepEnhancer#4683

Open
gesposito wants to merge 1 commit into
mobxjs:mainfrom
gesposito:perf/deep-enhancer-primitive-fast-path
Open

perf: fast-path primitives in deepEnhancer#4683
gesposito wants to merge 1 commit into
mobxjs:mainfrom
gesposito:perf/deep-enhancer-primitive-fast-path

Conversation

@gesposito

Copy link
Copy Markdown

What / why

deepEnhancer runs on every value that enters a deep observable — including every scalar write. For a primitive, it currently walks the full chain of type checks (isObservable, Array.isArray, isPlainObject, isES6Map, isES6Set, typeof function) before falling through to return v. Primitives can never be made observable, so this PR returns them immediately:

// primitives can never be made observable; skip the type checks below
if (v === null || (typeof v !== "object" && typeof v !== "function")) {
    return v
}

Functions are deliberately excluded from the early return so they still get wrapped into autoAction/flow. Behavior is unchanged for every input class — primitives previously fell through all checks and returned as-is.

Benchmarks

From npm -w mobx run test:performance (Node 26, macOS), alternating A/B runs against main with a rebuild on each side. Consistent across the proxy and legacy suites and across repeated runs:

Benchmark main this PR
Create array (1M primitives) 39–44 ms 10 ms
Set: setting/deleting 10k props × 1000 ~1750 ms ~1360 ms (−22%)
Map: setting/deleting 100 props × 1000 17 ms 12–13 ms (−25%)

All other benchmarks are within run-to-run noise.

Tests

Full suite passes. Added two tests pinning the fast-path boundary in makereactive.js:

  • every primitive type (number, string, boolean, null, undefined, symbol, bigint) passes through the deep enhancer unchanged
  • a function assigned to a deep observable property after creation is still wrapped into an autoAction

A changeset (patch bump for mobx) is included.

Code change checklist

  • Added/updated unit tests
  • Updated /docs. For new functionality, at least API.md should be updated — N/A, internal change only, no public API or documented behavior affected
  • Verified that there is no significant performance drop (npm -w mobx run test:performance)

@changeset-bot

changeset-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 342d8f9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mobx Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kubk kubk mentioned this pull request Jul 10, 2026
17 tasks
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