[Vue3] computed.globalVersoin

In Vue 3’s reactivity engine (as of v3.5), globalVersion is a single, ever‐incrementing counter that bumps whenever any reactive value changes. Each computed ref (ComputedRefImpl) carries its own computed.globalVersion. When you access a computed, Vue first checks:

if (computed.globalVersion === globalVersion) {
  // no reactive dependency has changed
  // skip running the computed’s getter
  return
}

If they differ, Vue knows something in the entire app has mutated since the last time that computed ran, so it (1) updates computed.globalVersion to match globalVersion, (2) re-runs the getter, and (3) only if the result changed does it bump the computed’s own dep.version to notify dependents. This global/version‐based gating lets Vue cheaply skip computed getters when nothing relevant has mutated.

posted @ 2025-06-23 02:26  Zhentiw  阅读(4)  评论(0)    收藏  举报