perf: improve directive runtime performance

This commit is contained in:
Evan You
2019-10-26 16:00:07 -04:00
parent 6c7787db7b
commit 07ce2c5fa7
3 changed files with 86 additions and 66 deletions

View File

@@ -66,7 +66,10 @@ export const vModelText: ObjectDirective<
addEventListener(el, 'change', onCompositionEnd)
}
},
beforeUpdate(el, { value, modifiers: { trim, number } }) {
beforeUpdate(el, { value, oldValue, modifiers: { trim, number } }) {
if (value === oldValue) {
return
}
if (document.activeElement === el) {
if (trim && el.value.trim() === value) {
return
@@ -107,15 +110,17 @@ export const vModelCheckbox: ObjectDirective<HTMLInputElement> = {
function setChecked(
el: HTMLInputElement,
{ value }: DirectiveBinding,
{ value, oldValue }: DirectiveBinding,
vnode: VNode
) {
// store the v-model value on the element so it can be accessed by the
// change listener.
;(el as any)._modelValue = value
el.checked = isArray(value)
? looseIndexOf(value, vnode.props!.value) > -1
: !!value
if (isArray(value)) {
el.checked = looseIndexOf(value, vnode.props!.value) > -1
} else if (value !== oldValue) {
el.checked = !!value
}
}
export const vModelRadio: ObjectDirective<HTMLInputElement> = {
@@ -126,8 +131,10 @@ export const vModelRadio: ObjectDirective<HTMLInputElement> = {
assign(getValue(el))
})
},
beforeUpdate(el, { value }, vnode) {
el.checked = looseEqual(value, vnode.props!.value)
beforeUpdate(el, { value, oldValue }, vnode) {
if (value !== oldValue) {
el.checked = looseEqual(value, vnode.props!.value)
}
}
}