Skip to content

Commit 7ffa77f

Browse files
committed
optimize instance hook event lookup
1 parent b97cd0b commit 7ffa77f

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

flow/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ declare interface Component {
6464
_isBeingDestroyed: boolean;
6565
_vnode: ?VNode;
6666
_staticTrees: ?Array<VNode>;
67+
_hasHookEvent: boolean;
6768

6869
// private methods
6970
// lifecycle

src/core/instance/events.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { updateListeners } from '../vdom/helpers/index'
55

66
export function initEvents (vm: Component) {
77
vm._events = Object.create(null)
8+
vm._hasHookEvent = false
89
// init parent attached events
910
const listeners = vm.$options._parentListeners
1011
if (listeners) {
@@ -36,9 +37,15 @@ export function updateComponentListeners (
3637
}
3738

3839
export function eventsMixin (Vue: Class<Component>) {
40+
const hookRE = /^hook:/
3941
Vue.prototype.$on = function (event: string, fn: Function): Component {
4042
const vm: Component = this
4143
;(vm._events[event] || (vm._events[event] = [])).push(fn)
44+
// optimize hook:event cost by using a boolean flag marked at registration
45+
// instead of a hash lookup
46+
if (hookRE.test(event)) {
47+
vm._hasHookEvent = true
48+
}
4249
return vm
4350
}
4451

@@ -87,7 +94,7 @@ export function eventsMixin (Vue: Class<Component>) {
8794
let cbs = vm._events[event]
8895
if (cbs) {
8996
cbs = cbs.length > 1 ? toArray(cbs) : cbs
90-
const args = arguments.length > 1 ? toArray(arguments, 1) : void 0
97+
const args = toArray(arguments, 1)
9198
for (let i = 0, l = cbs.length; i < l; i++) {
9299
cbs[i].apply(vm, args)
93100
}

src/core/instance/lifecycle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,7 @@ export function callHook (vm: Component, hook: string) {
211211
handlers[i].call(vm)
212212
}
213213
}
214-
vm.$emit('hook:' + hook)
214+
if (vm._hasHookEvent) {
215+
vm.$emit('hook:' + hook)
216+
}
215217
}

0 commit comments

Comments
 (0)