Skip to content
Created: February 23, 2023

✅ A table summarizing the differences ​

Data typesVue2 reactive processingVue3 reactive processingFundamental differences
ObjectObject.defineProperty hijacks propertiesProxy intercepts all operationsVue3 is more complete, faster, and supports deep operations
ArrayOnly rewrites 7 change methods, cannot monitor index and lengthProxy can monitor all operationsVue2 has a serious lack of monitoring capabilities, Vue3 is complete
Map❌ Not supported✔ Full support: get/set/has/delete/clear/traversalVue2 cannot handle it, Vue3 can respond to complex data structures
Set❌ Not supported✔ Full support: add/delete/clear/traversalVue2 cannot handle it, Vue3 supports iterative dependency tracking
WeakMap / WeakSet❌ Not supported✔ Supported (but weak collections do not trigger dependencies)Used for internal caching
Dependency TrackingCan only track property gettersCan track any behavior: iteration, size, keys, valuesVue3’s capabilities far exceed Vue2
PerformanceA lot of overhead, the larger the object, the slowerProxy constant timeVue3 has obvious advantages in big data structures

1. Vue2’s reactive system cannot perfectly proxy arrays ​

Vue2 can only do two things: ​

  1. Do defineProperty on array elements → Only detect existing indexes
  2. Override the array mutating function:
js
push
pop
shift
unshift
splice
sort
reverse

❌ Vue2 cannot monitor the situation ​

The following Vue2 all cannot be monitored:

js
arr[3] = 123   // 无法监听新增索引
arr.length = 1 // 无法监听 length 变化

👉 Because defineProperty cannot intercept index changes and cannot hijack length.

✔ Vue3’s array scheme is completely different: use Proxy ​

Vue3 uses:

js
Proxy(target, {
  get,
  set,
  has,
  ownKeys,
  deleteProperty,
})

So all behaviors of arrays can be intercepted:

Vue3 can monitor all situations: ​

js
arr[3] = 123        // ✔ 能监听
arr.length = 1      // ✔ 能监听
for (let i of arr)  // ✔ 遍历依赖建立
Object.keys(arr)    // ✔ ownKeys 拦截

👉 This makes Vue3’s array responsiveness almost the same as native JS.

2. Vue2 cannot handle Map / Set at all ​

Vue2's reactivity is based on defineProperty:

  • Map does not have key as attribute
  • Set has no index
  • There is no way to listen for add/delete/clear
  • Unable to capture iteration operations (forEach, for...of)

❌ Why can’t Vue2 do it? ​

Because there is no way to intercept the behavior of Map/Set at the attribute level.

3. ✔ Vue3 fully supports Map/Set (super powerful) ​

Vue3 uses Proxy, so it can listen to all methods of Map/Set.

Vue3 listens for all the following actions: ​

Map/Set APIDoes Vue3 support responsiveness
get✔ Create dependencies
set / add✔ trigger update
delete✔
clear✔
forEach✔
entries/keys/values✔
size✔ will trigger an update

Example: Vue3’s Map is responsive ​

js
const m = reactive(new Map())

m.set("name", "vue")  // 触发更新
m.get("name")         // 建立依赖
m.delete("name")      // 触发更新

Even:

js
for (const [k,v] of m) {
  // 也会建立迭代依赖
}

4. Vue3 has the concept of "iteration dependency", which Vue2 cannot do ​

Vue3 also establishes dependencies on such operations:

js
for...of
for...in
Object.keys()
Object.values()
Object.entries()
map.forEach()
map.size

This doesn't exist at all in Vue2.

5. weak collections (new support in Vue3) ​

Vue3 also supports:

js
WeakMap
WeakSet

But be careful:

  • Because key is a weak reference, so Dependencies are not persisted
  • Internally often used for caching, such as the dependency table of track/trigger

🎯 Core summary (enough for interviews) ​

The essence of the responsive difference between Vue2 vs Vue3: ​

FeaturesVue2(defineProperty)Vue3(Proxy)
Hijackable scopeAttribute level onlyAll operations on the entire object
Arraycan only hijack 7 methodsall operations
New attributeUnable to monitorAutomatically monitor
Delete attributeUnable to monitorAutomatically monitor
Map / Set❌ Unable to monitor✔ Full support
Iterative dependencies❌ Not supported✔ Supported
Deep structurePoor performanceFast
A large number of objectsExtremely slow initializationGood performance

📌 Summarize in one sentence ​

Vue3's reactive system is fundamentally rewritten; arrays, Maps, and Sets are all fully reactive, while Vue2 can only barely support objects and partial array APIs.