Backtestsvuerun-20261001-022054#15320
looseEqual() reports Map and Set instances with different contents as equal
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause. Each adds explicit Map/Set handling before the generic `Object.keys` path, so the `String(a) === String(b)` fallthrough no longer reports any two Maps or Sets as equal. Each also adds a v-model `<select>` regression test that would fail before the change.
The difference is Map semantics. Patch A's `looseCompareMaps` checks keys with `b.has(key)`, which is strict SameValueZero identity, and only compares values loosely. As a result, two Maps with structurally equal object keys (`new Map([[{id:1}, x]])` vs another `{id:1}` key) compare unequal. So do Maps keyed by `1` vs `'1'`, even though `looseEqual(1, '1')` is true and arrays and objects elsewhere get fully loose comparison. That makes Map behavior inconsistent with the function's own loose semantics. The issue asks for consistency with `looseEqual`'s semantics, using object elements in Sets as its example.
Patch B treats Map entries as `[key, value]` pairs and matches them one-to-one with `looseEqual`. Keys and values are therefore both compared loosely and order-insensitively. This matches the Set treatment, and B's tests cover it explicitly (object keys `{id:1}` vs `{id:'1'}`). Both use one-to-one greedy matching for Sets, which is sound given `looseEqual` behaves as an equivalence. Both place the checks safely: A before `isObject`, B inside the both-objects branch.
On tests, A's v-model test is slightly more thorough because it toggles the model back to `mapA`. B's looseEqual tests cover more edge cases (loose keys, a swapped-values map, `1`/`'1'` vs `1`/`2` sets). On craft, A is O(n) for Maps and a bit more readable, while B shares a single helper. These are minor.
B's more consistent loose semantics for Map keys tips the balance.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
2m 26s8 turns
Wall-clock time in the container
Full test suite
No new failures
3,692 passed · 6 skipped · 3,698 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
129vs 116 merged
Changed lines, ours vs merged
The issue
View on GitHubVue 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
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex 429163126..7592a7dd6 100644@@ -1009,6 +1009,37 @@ describe('vModel', () => { expect(bar.selected).toEqual(true) }) + it('should work with single select (option value is Map)', async () => {+ const mapA = new Map([['id', 1]])+ const mapB = new Map([['id', 2]])+ const model = ref<Map<string, number>>(mapB)+ const component = defineComponent({+ render() {+ return withVModel(+ h(+ 'select',+ {+ 'onUpdate:modelValue': (value: Map<string, number>) => {+ model.value = value+ },+ },+ [h('option', { value: mapA }), h('option', { value: mapB })],+ ),+ model.value,+ )+ },+ })+ render(h(component), root)+ await nextTick()++ const select = root.querySelector('select')+ expect(select.selectedIndex).toBe(1)++ model.value = mapA+ await nextTick()+ expect(select.selectedIndex).toBe(0)+ })+ it('multiple select (model is Array)', async () => { const component = defineComponent({ data() {packages/shared/__tests__/looseEqual.spec.tsindex 2dea58ce5..dc876106b 100644@@ -168,6 +168,60 @@ describe('utils/looseEqual', () => { expect(looseEqual(nestedObj1, nestedObj2)).toBe(false) }) + test('compares maps correctly', () => {+ const map1 = new Map([['a', 1]])++ // Identical map references+ expect(looseEqual(map1, map1)).toBe(true)+ // Different keys+ expect(looseEqual(new Map([['a', 1]]), new Map([['b', 2]]))).toBe(false)+ // Different sizes+ expect(looseEqual(new Map([['a', 1]]), new Map())).toBe(false)+ // Same key, different value+ expect(looseEqual(new Map([['a', 1]]), new Map([['a', 2]]))).toBe(false)+ // Keys in different orders+ expect(+ looseEqual(+ new Map([+ ['a', 1],+ ['b', 2],+ ]),+ new Map([+ ['b', 2],+ ['a', 1],+ ]),+ ),+ ).toBe(true)+ // Values compared loosely+ expect(+ looseEqual(new Map([['a', { x: 1 }]]), new Map([['a', { x: 1 }]])),+ ).toBe(true)+ // Map vs other types+ expect(looseEqual(new Map(), {})).toBe(false)+ expect(looseEqual(new Map(), new Set())).toBe(false)+ })++ test('compares sets correctly', () => {+ const set1 = new Set([1])++ // Identical set references+ expect(looseEqual(set1, set1)).toBe(true)+ // Different elements+ expect(looseEqual(new Set([1]), new Set([2]))).toBe(false)+ // Different sizes+ expect(looseEqual(new Set([1]), new Set())).toBe(false)+ // Elements in different orders+ expect(looseEqual(new Set([1, 2]), new Set([2, 1]))).toBe(true)+ // Elements compared loosely+ expect(looseEqual(new Set([{ a: 1 }]), new Set([{ a: 1 }]))).toBe(true)+ // Elements must match one-to-one+ expect(+ looseEqual(new Set([{ a: 1 }, { a: 1 }]), new Set([{ a: 1 }, { b: 2 }])),+ ).toBe(false)+ // Set vs array+ expect(looseEqual(new Set([1]), [1])).toBe(false)+ })+ test('compares different types correctly', () => { const obj1 = {} const obj2 = { a: 1 }packages/shared/src/looseEqual.tsindex 9e7176721..94b08211c 100644@@ -1,4 +1,11 @@-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 +16,31 @@ function looseCompareArrays(a: any[], b: any[]) { return equal } +function looseCompareMaps(a: Map<any, any>, b: Map<any, any>) {+ if (a.size !== b.size) return false+ for (const [key, value] of a) {+ if (!b.has(key) || !looseEqual(value, b.get(key))) return false+ }+ return true+}++function looseCompareSets(a: Set<any>, b: Set<any>) {+ if (a.size !== b.size) return false+ const bValues = Array.from(b)+ const used: boolean[] = []+ for (const aValue of a) {+ let found = false+ for (let i = 0; i < bValues.length; i++) {+ if (!used[i] && looseEqual(aValue, bValues[i])) {+ used[i] = found = true+ break+ }+ }+ if (!found) return false+ }+ return true+}+ export function looseEqual(a: any, b: any): boolean { if (a === b) return true let aValidType = isDate(a)@@ -26,6 +58,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) {
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) {
What the agent reported
looseEqual() now compares Map and Set by their contents, so <select v-model> with Map option values selects the right option. The new regression tests failed before the fix and pass after it, and 445 tests across the nearby suites pass. Nothing is committed; all changes are in the working tree.
What was wrong: looseEqual had no branch for Map or Set, so they were handled like plain objects. Object.keys() returns [] for both, so no entries were ever compared, and the function ended at String(a) === String(b). That is always '[object Map]' or '[object Set]', so any two Maps, or any two Sets, came out equal. In setSelected, that made the first option with a Map value match every time.
What I changed:
- Fix (
packages/shared/src/looseEqual.ts): two new branches, placed before the generic object branch and following the existing Date/Array pattern. They use theisMapandisSethelpers that already exist, plus two small helpers: - Maps: equal only if they are the same size and every key in one is in the other with a loosely equal value. Key order doesn't matter, and keys must be the same key (identity), not just look alike.
- Sets: equal only if they are the same size and each element pairs with a different loosely equal element of the other Set, in any order. So
new Set([{a:1}])equalsnew Set([{a:1}]), butSet([{a:1},{a:1}])does not equalSet([{a:1},{b:2}]). - A Map or Set compared with any other type (Map vs Set, plain object, array) is unequal.
- Tests:
packages/shared/__tests__/looseEqual.spec.ts: two new tests,compares maps correctlyandcompares sets correctly, covering the report's three examples plus the cases above.packages/runtime-dom/__tests__/directives/vModel.spec.ts: new testshould work with single select (option value is Map). It is the report'smapA/mapBexample and checks thatselectedIndexis1, then0after switching the model tomapA.
Test evidence:
- Before the fix: all three new tests failed —
looseEqual(Map a:1, Map b:2)returnedtrue,looseEqual(Set [1], Set [2])returnedtrue, andselectedIndexwas0instead of1. The other 51 tests in those two files passed. - After the fix: both files pass, 54 of 54.
- Wider run (
./bt-test packages/shared packages/runtime-dom/__tests__/directives packages/server-renderer/__tests__ packages/runtime-core/__tests__/rendererComponent.spec.ts): 28 files, 445 tests, all passed. No existing test was modified.
Other code affected: looseEqual and looseIndexOf are also used by:
v-modelon checkboxes, radios and selects (vModel.ts);- server-side rendering of
v-model(ssrVModelHelpers.ts); - the Vue 2 compatibility helpers
_qand_i; - the style-prop change check in
componentRenderUtils.ts.
Only comparisons involving a Map or Set behave differently; everything else follows the same path as before. The Set-as-model <select multiple> logic in vModel.ts has its own check and doesn't go through looseEqual. The reviewer found no blocking issues.
Requirements:
looseEqual(new Map([['a',1]]), new Map([['b',2]]))should be false: done and tested.looseEqual(new Set([1]), new Set([2]))should be false: done and tested.looseEqual(new Map([['a',1]]), new Map())should be false: done and tested.v-modelselect withmapA/mapBshould select index 1: done and tested invModel.spec.ts.- Add explicit branches mirroring Date/Symbol, using the existing
isMap/isSet: done. - Set compares loosely by content (the
{ a: 1 }example): done, with one-to-one pairing, and tested. - Open question: should Map comparison ignore key order? Nobody was available to confirm, so I chose yes, since Maps with the same entries should be equal. Tested.
- Open question: should Sets compare loosely or strictly? I chose loosely, to match how
looseEqualtreats arrays and objects. Tested. - Add Map/Set cases to
looseEqual.spec.ts: done.
Map keys are matched by identity, so two Maps keyed by separate but identical-looking objects still compare unequal. The Set check pairs each element with the first match it finds without retrying. That could misjudge only some unusual mixes of strings and numbers, which looseEqual treats as equal by their text (e.g. 1 and '1'). Neither limitation is covered by a test.