Skip to content

Commit 485ca58

Browse files
committed
optimization: skip observe in v-for fragment value
1 parent 69be428 commit 485ca58

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/directives/public/for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ const vFor = {
237237
// for two-way binding on alias
238238
scope.$forContext = this
239239
// define scope properties
240-
defineReactive(scope, alias, value)
240+
defineReactive(scope, alias, value, true /* do not observe */)
241241
defineReactive(scope, '$index', index)
242242
if (key) {
243243
defineReactive(scope, '$key', key)

src/observer/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,10 @@ export function observe (value, vm) {
171171
* @param {Object} obj
172172
* @param {String} key
173173
* @param {*} val
174+
* @param {Boolean} doNotObserve
174175
*/
175176

176-
export function defineReactive (obj, key, val) {
177+
export function defineReactive (obj, key, val, doNotObserve) {
177178
var dep = new Dep()
178179

179180
var property = Object.getOwnPropertyDescriptor(obj, key)
@@ -185,7 +186,13 @@ export function defineReactive (obj, key, val) {
185186
var getter = property && property.get
186187
var setter = property && property.set
187188

188-
var childOb = observe(val)
189+
// if doNotObserve is true, only use the child value observer
190+
// if it already exists, and do not attempt to create it.
191+
// this allows freezing a large object from the root and
192+
// avoid unnecessary observation inside v-for fragments.
193+
var childOb = doNotObserve
194+
? typeof val === 'object' && val.__ob__
195+
: observe(val)
189196
Object.defineProperty(obj, key, {
190197
enumerable: true,
191198
configurable: true,
@@ -215,7 +222,9 @@ export function defineReactive (obj, key, val) {
215222
} else {
216223
val = newVal
217224
}
218-
childOb = observe(newVal)
225+
childOb = doNotObserve
226+
? typeof newVal === 'object' && newVal.__ob__
227+
: observe(newVal)
219228
dep.notify()
220229
}
221230
})

0 commit comments

Comments
 (0)