Skip to content

Commit

Permalink
feat: config.performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 20, 2017
1 parent f916bcf commit 689c107
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
1 change: 1 addition & 0 deletions flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare interface Component {

// private properties
_uid: number;
_name: string; // this only exists in dev mode
_isVue: true;
_self: Component;
_renderProxy: Component;
Expand Down
6 changes: 6 additions & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Config = {
optionMergeStrategies: { [key: string]: Function };
silent: boolean;
productionTip: boolean;
performance: boolean;
devtools: boolean;
errorHandler: ?Function;
ignoredElements: Array<string>;
Expand Down Expand Up @@ -44,6 +45,11 @@ const config: Config = {
*/
devtools: process.env.NODE_ENV !== 'production',

/**
* Whether to record perf
*/
performance: process.env.NODE_ENV !== 'production',

/**
* Error handler for watcher errors
*/
Expand Down
15 changes: 14 additions & 1 deletion src/core/instance/init.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/* @flow */

import config from '../config'
import { perf } from '../util/perf'
import { initProxy } from './proxy'
import { initState } from './state'
import { initRender } from './render'
import { initEvents } from './events'
import { initInjections } from './inject'
import { initLifecycle, callHook } from './lifecycle'
import { mergeOptions } from '../util/index'
import { mergeOptions, formatComponentName } from '../util/index'

let uid = 0

export function initMixin (Vue: Class<Component>) {
Vue.prototype._init = function (options?: Object) {
if (process.env.NODE_ENV !== 'production' && config.performance && perf) {
perf.mark('init')
}

const vm: Component = this
// a uid
vm._uid = uid++
Expand Down Expand Up @@ -45,6 +51,13 @@ export function initMixin (Vue: Class<Component>) {
initState(vm)
initInjections(vm)
callHook(vm, 'created')

if (process.env.NODE_ENV !== 'production' && config.performance && perf) {
vm._name = formatComponentName(vm, false)
perf.mark('init end')
perf.measure(`${vm._name} init`, 'init', 'init end')
}

if (vm.$options.el) {
vm.$mount(vm.$options.el)
}
Expand Down
39 changes: 35 additions & 4 deletions src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
/* @flow */

import config from '../config'
import { perf } from '../util/perf'
import Watcher from '../observer/watcher'
import { resetRefs } from '../vdom/modules/ref'
import { createEmptyVNode } from '../vdom/vnode'
import { observerState } from '../observer/index'
import { updateComponentListeners } from './events'
import { resolveSlots } from './render-helpers/resolve-slots'
import { warn, validateProp, remove, noop, emptyObject, handleError } from '../util/index'

import {
warn,
noop,
remove,
handleError,
emptyObject,
validateProp
} from '../util/index'

export let activeInstance: any = null

Expand Down Expand Up @@ -149,10 +159,31 @@ export function mountComponent (
}
}
callHook(vm, 'beforeMount')
vm._watcher = new Watcher(vm, function updateComponent () {
vm._update(vm._render(), hydrating)
}, noop)

let updateComponent
if (process.env.NODE_ENV !== 'production' && config.performance && perf) {
updateComponent = () => {
const name = vm._name
const startTag = `start ${name}`
const endTag = `end ${name}`
perf.mark(startTag)
const vnode = vm._render()
perf.mark(endTag)
perf.measure(`${name} render`, startTag, endTag)
perf.mark(startTag)
vm._update(vnode, hydrating)
perf.mark(endTag)
perf.measure(`${name} patch`, startTag, endTag)
}
} else {
updateComponent = () => {
vm._update(vm._render(), hydrating)
}
}

vm._watcher = new Watcher(vm, updateComponent, noop)
hydrating = false

// manually mounted instance, call mounted on self
// mounted is called for render-created child components in its inserted hook
if (vm.$vnode == null) {
Expand Down
23 changes: 17 additions & 6 deletions src/core/util/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ let formatComponentName

if (process.env.NODE_ENV !== 'production') {
const hasConsole = typeof console !== 'undefined'
const classifyRE = /(?:^|[-_/])(\w)/g
const classify = str => str
.replace(classifyRE, c => c.toUpperCase())
.replace(/-/g, '')

warn = (msg, vm) => {
if (hasConsole && (!config.silent)) {
Expand All @@ -15,21 +19,28 @@ if (process.env.NODE_ENV !== 'production') {
}
}

formatComponentName = vm => {
formatComponentName = (vm, includeFile) => {
if (vm.$root === vm) {
return 'root instance'
return '<Root>'
}
const name = vm._isVue
let name = vm._isVue
? vm.$options.name || vm.$options._componentTag
: vm.name

const file = vm._isVue && vm.$options.__file
if (!name && file) {
const match = file.match(/([^/\\]+)\.vue$/)
name = match && match[1]
}

return (
(name ? `component <${name}>` : `anonymous component`) +
(vm._isVue && vm.$options.__file ? ` at ${vm.$options.__file}` : '')
(name ? `<${classify(name)}>` : `<Anonymous>`) +
(file && includeFile !== false ? file : '')
)
}

const formatLocation = str => {
if (str === 'anonymous component') {
if (str === `<Anonymous>`) {
str += ` - use the "name" option for better debugging messages.`
}
return `\n(found in ${str})`
Expand Down
10 changes: 10 additions & 0 deletions src/core/util/perf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { inBrowser } from './env'

export let perf

if (process.env.NODE_ENV !== 'production') {
perf = inBrowser && window.performance
if (perf && (!perf.mark || !perf.measure)) {
perf = undefined
}
}
13 changes: 12 additions & 1 deletion src/entries/web-runtime-with-compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* @flow */

import Vue from './web-runtime'
import { warn, cached } from 'core/util/index'
import config from 'core/config'
import { perf } from 'core/util/perf'
import { query } from 'web/util/index'
import { warn, cached } from 'core/util/index'
import { shouldDecodeNewlines } from 'web/util/compat'
import { compileToFunctions } from 'web/compiler/index'

Expand Down Expand Up @@ -54,13 +56,22 @@ Vue.prototype.$mount = function (
template = getOuterHTML(el)
}
if (template) {
if (process.env.NODE_ENV !== 'production' && config.performance && perf) {
perf.mark('compile')
}

const { render, staticRenderFns } = compileToFunctions(template, {
warn: msg => warn(msg, this),
shouldDecodeNewlines,
delimiters: options.delimiters
}, this)
options.render = render
options.staticRenderFns = staticRenderFns

if (process.env.NODE_ENV !== 'production' && config.performance && perf) {
perf.mark('compile end')
perf.measure(`${this._name} compile`, 'compile', 'compile end')
}
}
}
return mount.call(this, el, hydrating)
Expand Down

0 comments on commit 689c107

Please sign in to comment.