Backtestsvuerun-20261021-133314#15320

Issue #15320 Merged fix PR #15328 Run 21 Oct 2026 · run-20261021-133314 vuejs/core

looseEqual() reports Map and Set instances with different contents as equal

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause the same way: they add explicit `isMap`/`isSet` branches to `looseEqual` with order-insensitive, loosely-compared, one-to-one matching of entries, and both pass all eight independent tests that failed on base. Neither is a symptom mask. Correctness is effectively equal; both handle size mismatch, duplicate-looking members (via a used/matched array), Map-vs-Set, and Map-vs-plain-object. Patch B's implementation is simpler (one `looseCompareCollections` that reuses array comparison for `[key, value]` entries), which is a point in its favour, while Patch A's `looseCompareEntries` with three callbacks is heavier, though its identical-reference fast path is a sensible optimisation. Where they differ meaningfully is placement and tests. Patch A inserts the branches alongside the existing Date/Symbol/Array branches before the generic `isObject` check, exactly the pattern the issue suggests and the file already uses; Patch B nests them inside the object branch, which works but breaks the file's flat type-dispatch convention. Patch A's tests are substantially more thorough: empty maps/sets in both argument orders, key/order insensitivity, nested maps, loose object keys, the one-to-one matching rule, string-vs-collection mismatches, plus v-model tests covering select changes over time and radio/checkbox, which the issue cites as the user-facing symptom. Patch B's tests cover the core cases but are thinner and its v-model test checks only initial selection. On balance A edges out B on tests and convention consistency, with correctness tied and B's simpler helper not enough to offset that.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

5m 52s

Wall-clock time in the container

Full test suite

No new failures

3,693 passed · 6 skipped · 3,699 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

227vs 116 merged

Changed lines, ours vs merged

Vue version

3.5.x / main (verified at a2b40db)

Link to minimal reproduction

Reproducible directly against the repo (see below) — looseEqual is not exported publicly, so the SFC Playground can only show the v-model symptom, not the root cause.

Steps to reproduce

looseEqual() in packages/shared/src/looseEqual.ts reports unrelated Map and Set instances as equal:

import { looseEqual } from '@vue/shared'

looseEqual(new Map([['a', 1]]), new Map([['b', 2]])) // true, expected false
looseEqual(new Set([1]), new Set([2]))               // true, expected false
looseEqual(new Map([['a', 1]]), new Map())           // true, expected false

This surfaces to users through v-model on <select>. The following selects the wrong option:

const mapA = new Map([['id', 1]])
const mapB = new Map([['id', 2]])
const selected = ref(mapB)
<select v-model="selected">
  <option :value="mapA">A</option>
  <option :value="mapB">B</option>
</select>

el.selectedIndex is 0 (option A) when it should be 1, because looseEqual(mapA, mapB) returns true and the loop in setSelected stops at the first option.

What is expected?

Map/Set values with different contents compare as not equal, so v-model selects the option that actually matches.

What is actually happening?

Map and Set fall through to the generic object branch. Object.keys() on either is always [], so the key-count check passes and the for...in loop body never executes — entries are never compared. Execution then reaches the final line:

return String(a) === String(b)

Every Map stringifies to '[object Map]' and every Set to '[object Set]', so any two of the same type are reported equal.

Note this differs from Date and Symbol, which are given explicit branches above, and from arrays and plain objects, which compare correctly:

looseEqual([1], [2])       // false, correct
looseEqual({ a: 1 }, { a: 2 }) // false, correct

packages/shared/__tests__/looseEqual.spec.ts has 13 cases but none for Map/Set, which is why this hasn't been caught.

Possible fix

Add explicit branches before the generic object check, mirroring the existing Date/Symbol pattern. isMap and isSet already exist in packages/shared/src/general.ts. Set needs content comparison rather than reference lookup to stay consistent with looseEqual's semantics (e.g. new Set([{ a: 1 }]) vs new Set([{ a: 1 }])).

Worth confirming the intended semantics first: should Map comparison be key-order-insensitive, and should Set compare loosely (looseEqual per element) or strictly? Happy to open a PR with tests once the direction is confirmed.

System Info

Vue: 3.5.x / main @ a2b40db

Any additional comments?

Unrelated to #15298, which touches only vModel.ts.

The two patches

Our patch+226 −1
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 429163126..9ade206f6 100644@@ -1432,6 +1432,76 @@ describe('vModel', () => {     expect(input.selectedIndex).toBe(1)   }) +  it('should select the option whose Map/Set value matches the model', async () => {+    const mapA = new Map([['id', 1]])+    const mapB = new Map([['id', 2]])+    const setA = new Set([1])+    const setB = new Set([2])+    const value = ref<any>(mapB)+    const component = defineComponent({+      render() {+        return withVModel(+          h(+            'select',+            { 'onUpdate:modelValue': (v: any) => (value.value = v) },+            [+              h('option', { value: mapA }),+              h('option', { value: mapB }),+              h('option', { value: setA }),+              h('option', { value: setB }),+            ],+          ),+          value.value,+        )+      },+    })+    render(h(component), root)+    const input = root.querySelector('select') as HTMLSelectElement+    expect(input.selectedIndex).toBe(1)++    value.value = setB+    await nextTick()+    expect(input.selectedIndex).toBe(3)++    value.value = new Map([['id', 1]])+    await nextTick()+    expect(input.selectedIndex).toBe(0)+  })++  it('should check only the radio/checkbox whose Map value matches the model', async () => {+    const mapA = new Map([['id', 1]])+    const mapB = new Map([['id', 2]])+    const radio = ref<any>(mapB)+    const checkbox = ref<any>([mapB])+    const component = defineComponent({+      render() {+        return [+          withVModel(+            h('input', { type: 'radio', class: 'ra', value: mapA }),+            radio.value,+          ),+          withVModel(+            h('input', { type: 'radio', class: 'rb', value: mapB }),+            radio.value,+          ),+          withVModel(+            h('input', { type: 'checkbox', class: 'ca', value: mapA }),+            checkbox.value,+          ),+          withVModel(+            h('input', { type: 'checkbox', class: 'cb', value: mapB }),+            checkbox.value,+          ),+        ]+      },+    })+    render(h(component), root)+    expect(root.querySelector('.ra').checked).toBe(false)+    expect(root.querySelector('.rb').checked).toBe(true)+    expect(root.querySelector('.ca').checked).toBe(false)+    expect(root.querySelector('.cb').checked).toBe(true)+  })+   it('should resync when a multiple select becomes single during the pending update', async () => {     const component = defineComponent({       data() {packages/shared/__tests__/looseEqual.spec.tsindex 2dea58ce5..f92282f06 100644@@ -233,4 +233,88 @@ describe('utils/looseEqual', () => {     expect(looseEqual(arr1, arr5)).toBe(false)     expect(looseEqual(arr5, arr1)).toBe(false)   })++  test('compares maps correctly', () => {+    expect(looseEqual(new Map([['a', 1]]), new Map([['a', 1]]))).toBe(true)+    expect(looseEqual(new Map([['a', 1]]), new Map([['b', 2]]))).toBe(false)+    expect(looseEqual(new Map([['a', 1]]), new Map([['a', 2]]))).toBe(false)+    expect(looseEqual(new Map([['a', 1]]), new Map())).toBe(false)+    expect(looseEqual(new Map(), new Map([['a', 1]]))).toBe(false)+    expect(looseEqual(new Map(), new Map())).toBe(true)+    // key order does not matter+    expect(+      looseEqual(+        new Map<string, number>([+          ['a', 1],+          ['b', 2],+        ]),+        new Map<string, number>([+          ['b', 2],+          ['a', 1],+        ]),+      ),+    ).toBe(true)+    // values are compared loosely+    expect(+      looseEqual(new Map([['a', { x: 1 }]]), new Map([['a', { x: 1 }]])),+    ).toBe(true)+    expect(+      looseEqual(new Map([['a', { x: 1 }]]), new Map([['a', { x: 2 }]])),+    ).toBe(false)+    // object keys are compared loosely+    expect(looseEqual(new Map([[{ k: 1 }, 1]]), new Map([[{ k: 1 }, 1]]))).toBe(+      true,+    )+    expect(looseEqual(new Map([[{ k: 1 }, 1]]), new Map([[{ k: 2 }, 1]]))).toBe(+      false,+    )+    // identical keys may still pair with other loosely equal keys+    expect(+      looseEqual(+        new Map<any, string>([+          [1, 'x'],+          ['1', 'y'],+        ]),+        new Map<any, string>([+          [1, 'y'],+          ['1', 'x'],+        ]),+      ),+    ).toBe(true)+    // nested+    expect(+      looseEqual(+        new Map([['a', new Map([['b', 1]])]]),+        new Map([['a', new Map([['b', 2]])]]),+      ),+    ).toBe(false)+    // different types+    expect(looseEqual(new Map(), {})).toBe(false)+    expect(looseEqual({}, new Map())).toBe(false)+    expect(looseEqual(new Map(), new Set())).toBe(false)+    expect(looseEqual(new Map(), [])).toBe(false)+    expect(looseEqual(new Map(), '[object Map]')).toBe(false)+  })++  test('compares sets correctly', () => {+    expect(looseEqual(new Set([1]), new Set([1]))).toBe(true)+    expect(looseEqual(new Set([1]), new Set([2]))).toBe(false)+    expect(looseEqual(new Set([1]), new Set())).toBe(false)+    expect(looseEqual(new Set(), new Set([1]))).toBe(false)+    expect(looseEqual(new Set(), new Set())).toBe(true)+    // order does not matter+    expect(looseEqual(new Set([1, 2]), new Set([2, 1]))).toBe(true)+    // members are compared loosely+    expect(looseEqual(new Set([{ a: 1 }]), new Set([{ a: 1 }]))).toBe(true)+    expect(looseEqual(new Set([{ a: 1 }]), new Set([{ a: 2 }]))).toBe(false)+    expect(looseEqual(new Set(['1']), new Set([1]))).toBe(true)+    // each member is matched at most once+    expect(looseEqual(new Set([[1], [1]]), new Set([[1], [2]]))).toBe(false)+    expect(looseEqual(new Set([[1], [2]]), new Set([[2], [1]]))).toBe(true)+    // different types+    expect(looseEqual(new Set(), [])).toBe(false)+    expect(looseEqual([], new Set())).toBe(false)+    expect(looseEqual(new Set(), {})).toBe(false)+    expect(looseEqual(new Set(), '[object Set]')).toBe(false)+  }) })packages/shared/src/looseEqual.tsindex 9e7176721..9becf161c 100644@@ -1,4 +1,4 @@-import { isArray, isDate, isObject, isSymbol } from './general'+import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general'  function looseCompareArrays(a: any[], b: any[]) {   if (a.length !== b.length) return false@@ -9,6 +9,67 @@ function looseCompareArrays(a: any[], b: any[]) {   return equal } +/**+ * Compares two collections of entries, pairing each entry of `a` with a+ * distinct entry of `b`. Identical references are paired first so that+ * order and duplicate-looking members do not affect the result.+ */+function looseCompareEntries(+  a: any[],+  b: any[],+  bHas: (key: any) => boolean,+  getKey: (entry: any) => any,+  entryEqual: (aEntry: any, bEntry: any) => boolean,+) {+  if (a.length !== b.length) return false+  const used = new Array(b.length).fill(false)+  const pending: any[] = []+  for (const aEntry of a) {+    const key = getKey(aEntry)+    if (bHas(key)) {+      const index = b.findIndex(+        (bEntry, i) => !used[i] && getKey(bEntry) === key,+      )+      if (index > -1 && entryEqual(aEntry, b[index])) {+        used[index] = true+        continue+      }+    }+    pending.push(aEntry)+  }+  for (const aEntry of pending) {+    const index = b.findIndex(+      (bEntry, i) => !used[i] && entryEqual(aEntry, bEntry),+    )+    if (index < 0) return false+    used[index] = true+  }+  return true+}++function looseCompareMaps(a: Map<any, any>, b: Map<any, any>) {+  if (a.size !== b.size) return false+  return looseCompareEntries(+    Array.from(a),+    Array.from(b),+    key => b.has(key),+    entry => entry[0],+    (aEntry, bEntry) =>+      looseEqual(aEntry[0], bEntry[0]) && looseEqual(aEntry[1], bEntry[1]),+  )+}++function looseCompareSets(a: Set<any>, b: Set<any>) {+  if (a.size !== b.size) return false+  return looseCompareEntries(+    Array.from(a),+    Array.from(b),+    value => b.has(value),+    value => value,+    looseEqual,+  )+}+ export function looseEqual(a: any, b: any): boolean {   if (a === b) return true   let aValidType = isDate(a)@@ -26,6 +87,16 @@ export function looseEqual(a: any, b: any): boolean {   if (aValidType || bValidType) {     return aValidType && bValidType ? looseCompareArrays(a, b) : false   }+  aValidType = isMap(a)+  bValidType = isMap(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType ? looseCompareMaps(a, b) : false+  }+  aValidType = isSet(a)+  bValidType = isSet(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType ? looseCompareSets(a, b) : false+  }   aValidType = isObject(a)   bValidType = isObject(b)   if (aValidType || bValidType) { 
Merged fix · PR #15328+115 −1
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 429163126..a94cb8e0d 100644@@ -1874,4 +1874,32 @@ describe('vModel', () => {     await nextTick()     expect(foo.checked).toEqual(false)   })++  // #15320+  it('selects the matching option when values are maps', () => {+    const mapA = new Map([['id', 1]])+    const mapB = new Map([['id', 2]])+    const value = ref(mapB)+    const component = defineComponent(+      () => () =>+        withVModel(+          h(+            'select',+            {+              'onUpdate:modelValue': (val: Map<string, number>) =>+                (value.value = val),+            },+            [+              h('option', { value: mapA }, 'A'),+              h('option', { value: mapB }, 'B'),+            ],+          ),+          value.value,+        ),+    )++    render(h(component), root)++    expect(root.querySelector('select').selectedIndex).toBe(1)+  }) })packages/shared/__tests__/looseEqual.spec.tsindex 2dea58ce5..777f3cfad 100644@@ -233,4 +233,59 @@ describe('utils/looseEqual', () => {     expect(looseEqual(arr1, arr5)).toBe(false)     expect(looseEqual(arr5, arr1)).toBe(false)   })++  test('compares maps correctly', () => {+    const map = new Map<any, any>([+      [{ id: 1 }, { value: '1' }],+      ['foo', 'bar'],+    ])++    expect(looseEqual(map, map)).toBe(true)+    expect(+      looseEqual(+        map,+        new Map<any, any>([+          ['foo', 'bar'],+          [{ id: '1' }, { value: 1 }],+        ]),+      ),+    ).toBe(true)+    expect(looseEqual(map, new Map([['foo', 'bar']]))).toBe(false)+    expect(looseEqual(new Map([['a', 1]]), new Map([['b', 2]]))).toBe(false)+    expect(+      looseEqual(+        new Map<any, any>([+          ['a', 1],+          ['b', 2],+        ]),+        new Map<any, any>([+          ['a', 2],+          ['b', 1],+        ]),+      ),+    ).toBe(false)+    expect(+      looseEqual(+        new Map<any, any>([+          [1, 'value'],+          ['1', 'value'],+        ]),+        new Map<any, any>([+          [1, 'value'],+          [2, 'value'],+        ]),+      ),+    ).toBe(false)+  })++  test('compares sets correctly', () => {+    const set = new Set<any>([{ id: 1 }, 'foo'])++    expect(looseEqual(set, set)).toBe(true)+    expect(looseEqual(set, new Set(['foo', { id: '1' }]))).toBe(true)+    expect(looseEqual(set, new Set(['foo']))).toBe(false)+    expect(looseEqual(new Set([1]), new Set([2]))).toBe(false)+    expect(looseEqual(new Set<any>([1, '1']), new Set<any>([1, 2]))).toBe(false)+    expect(looseEqual(new Set(), new Map())).toBe(false)+  }) })packages/shared/src/looseEqual.tsindex 9e7176721..d037b0a57 100644@@ -1,4 +1,4 @@-import { isArray, isDate, isObject, isSymbol } from './general'+import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general'  function looseCompareArrays(a: any[], b: any[]) {   if (a.length !== b.length) return false@@ -9,6 +9,27 @@ function looseCompareArrays(a: any[], b: any[]) {   return equal } +function looseCompareCollections(+  a: Map<any, any> | Set<any>,+  b: Map<any, any> | Set<any>,+) {+  if (a.size !== b.size) return false+  const candidates = Array.from(b)+  const matched = new Uint8Array(candidates.length)+  for (const item of a) {+    let index = -1+    for (let i = 0; i < candidates.length; i++) {+      if (!matched[i] && looseEqual(item, candidates[i])) {+        index = i+        break+      }+    }+    if (index < 0) return false+    matched[index] = 1+  }+  return true+}+ export function looseEqual(a: any, b: any): boolean {   if (a === b) return true   let aValidType = isDate(a)@@ -32,6 +53,16 @@ export function looseEqual(a: any, b: any): boolean {     if (!aValidType || !bValidType) {       return false     }+    aValidType = isMap(a)+    bValidType = isMap(b)+    if (aValidType || bValidType) {+      return aValidType && bValidType ? looseCompareCollections(a, b) : false+    }+    aValidType = isSet(a)+    bValidType = isSet(b)+    if (aValidType || bValidType) {+      return aValidType && bValidType ? looseCompareCollections(a, b) : false+    }     const aKeysCount = Object.keys(a).length     const bKeysCount = Object.keys(b).length     if (aKeysCount !== bKeysCount) {