Skip to content

Commit e870e6c

Browse files
committed
move props definition to component prototype when possible
1 parent 406352b commit e870e6c

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/core/global-api/extend.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import config from '../config'
44
import { warn, mergeOptions } from '../util/index'
5-
import { defineComputed } from '../instance/state'
5+
import { defineComputed, proxy } from '../instance/state'
66

77
export function initExtend (Vue: GlobalAPI) {
88
/**
@@ -48,6 +48,12 @@ export function initExtend (Vue: GlobalAPI) {
4848
)
4949
Sub['super'] = Super
5050

51+
// For props and computed properties, we define the proxy getters on
52+
// the Vue instances at extension time, on the extended prototype. This
53+
// avoids Object.defineProperty calls for each instance created.
54+
if (Sub.options.props) {
55+
initProps(Sub)
56+
}
5157
if (Sub.options.computed) {
5258
initComputed(Sub)
5359
}
@@ -79,6 +85,13 @@ export function initExtend (Vue: GlobalAPI) {
7985
}
8086
}
8187

88+
function initProps (Comp) {
89+
const props = Comp.options.props
90+
for (const key in props) {
91+
proxy(Comp.prototype, `_props`, key)
92+
}
93+
}
94+
8295
function initComputed (Comp) {
8396
const computed = Comp.options.computed
8497
for (const key in computed) {

src/core/instance/state.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import {
2121
noop
2222
} from '../util/index'
2323

24+
const sharedPropertyDefinition = {
25+
enumerable: true,
26+
configurable: true,
27+
get: noop,
28+
set: noop
29+
}
30+
2431
export function initState (vm: Component) {
2532
vm._watchers = []
2633
const opts = vm.$options
@@ -71,7 +78,9 @@ function initProps (vm: Component, propsOptions: Object) {
7178
} else {
7279
defineReactive(props, key, value)
7380
}
74-
proxy(vm, props, key)
81+
if (!(key in vm)) {
82+
proxy(vm, `_props`, key)
83+
}
7584
}
7685
observerState.shouldConvert = true
7786
}
@@ -101,7 +110,7 @@ function initData (vm: Component) {
101110
vm
102111
)
103112
} else if (!isReserved(keys[i])) {
104-
proxy(vm, data, keys[i])
113+
proxy(vm, `_data`, keys[i])
105114
}
106115
}
107116
// observe data
@@ -128,28 +137,21 @@ function initComputed (vm: Component, computed: Object) {
128137
}
129138
}
130139

131-
const computedSharedDefinition = {
132-
enumerable: true,
133-
configurable: true,
134-
get: noop,
135-
set: noop
136-
}
137-
138140
export function defineComputed (target: any, key: string, userDef: Object | Function) {
139141
if (typeof userDef === 'function') {
140-
computedSharedDefinition.get = createComputedGetter(key)
141-
computedSharedDefinition.set = noop
142+
sharedPropertyDefinition.get = createComputedGetter(key)
143+
sharedPropertyDefinition.set = noop
142144
} else {
143-
computedSharedDefinition.get = userDef.get
145+
sharedPropertyDefinition.get = userDef.get
144146
? userDef.cache !== false
145147
? createComputedGetter(key)
146148
: userDef.get
147149
: noop
148-
computedSharedDefinition.set = userDef.set
150+
sharedPropertyDefinition.set = userDef.set
149151
? userDef.set
150152
: noop
151153
}
152-
Object.defineProperty(target, key, computedSharedDefinition)
154+
Object.defineProperty(target, key, sharedPropertyDefinition)
153155
}
154156

155157
function createComputedGetter (key) {
@@ -249,15 +251,12 @@ export function stateMixin (Vue: Class<Component>) {
249251
}
250252
}
251253

252-
function proxy (vm: Component, source: Object, key: string) {
253-
Object.defineProperty(vm, key, {
254-
configurable: true,
255-
enumerable: true,
256-
get: function proxyGetter () {
257-
return source[key]
258-
},
259-
set: function proxySetter (val) {
260-
source[key] = val
261-
}
262-
})
254+
export function proxy (target: Object, sourceKey: string, key: string) {
255+
sharedPropertyDefinition.get = function proxyGetter () {
256+
return this[sourceKey][key]
257+
}
258+
sharedPropertyDefinition.set = function proxySetter (val) {
259+
this[sourceKey][key] = val
260+
}
261+
Object.defineProperty(target, key, sharedPropertyDefinition)
263262
}

0 commit comments

Comments
 (0)