Bläddra i källkod

more tweak to render function caching

Evan You 10 år sedan
förälder
incheckning
065dc31e8a
2 ändrade filer med 29 tillägg och 36 borttagningar
  1. 3 14
      src/compiler/index.js
  2. 26 22
      src/entries/web-runtime-with-compiler.js

+ 3 - 14
src/compiler/index.js

@@ -3,21 +3,10 @@ import { optimize } from './optimizer/index'
 import { generate } from './codegen/index'
 import { directives } from './codegen/directives/index'
 
-const cache1 = Object.create(null)
-const cache2 = Object.create(null)
-
 export function compile (html, options) {
-  html = html.trim()
-  options = options || {}
-  const cache = options.preserveWhitespace ? cache1 : cache2
-  const hit = cache[html]
-  if (hit) {
-    return hit
-  } else {
-    const ast = parse(html, options)
-    optimize(ast)
-    return (cache[html] = generate(ast))
-  }
+  const ast = parse(html.trim(), options)
+  optimize(ast)
+  return generate(ast)
 }
 
 export function registerDirective (name, fn) {

+ 26 - 22
src/entries/web-runtime-with-compiler.js

@@ -1,22 +1,34 @@
 import config from '../runtime/config'
 import { compile } from '../compiler/index'
-import { query, warn } from '../runtime/util/index'
+import { query, warn, cached } from '../runtime/util/index'
 import Vue from './web-runtime'
 
-const mount = Vue.prototype.$mount
-const idTemplateCache = Object.create(null)
-const renderFunctionCache = Object.create(null)
+const idToTemplate = cached(id => query(id).innerHTML)
 
-function idToTemplate (id) {
-  const hit = idTemplateCache[id]
-  return hit || (idTemplateCache[id] = query(id).innerHTML)
-}
+const cache1 = Object.create(null)
+const cache2 = Object.create(null)
 
-function createRenderFn (code) {
-  const hit = renderFunctionCache[code]
-  return hit || (renderFunctionCache[code] = new Function(code))
+function createRenderFns (template) {
+  const preserveWhitespace = config.preserveWhitespace
+  const cache = preserveWhitespace ? cache1 : cache2
+  if (cache[template]) {
+    return cache[template]
+  }
+  const res = {}
+  const compiled = compile(template, { preserveWhitespace })
+  res.render = new Function(compiled.render)
+  const l = compiled.staticRenderFns.length
+  if (l) {
+    res.staticRenderFns = new Array(l)
+    for (let i = 0; i < l; i++) {
+      res.staticRenderFns[i] = new Function(compiled.staticRenderFns[i])
+    }
+  }
+  return (cache[template] = res)
 }
 
+const mount = Vue.prototype.$mount
+
 Vue.prototype.$mount = function (el) {
   el = el && query(el)
   const options = this.$options
@@ -37,17 +49,9 @@ Vue.prototype.$mount = function (el) {
       template = getOuterHTML(el)
     }
     if (template) {
-      const res = compile(template, {
-        preserveWhitespace: config.preserveWhitespace
-      })
-      options.render = createRenderFn(res.render)
-      const l = res.staticRenderFns.length
-      if (l) {
-        options.staticRenderFns = new Array(l)
-        for (let i = 0; i < l; i++) {
-          options.staticRenderFns[i] = createRenderFn(res.staticRenderFns[i])
-        }
-      }
+      const { render, staticRenderFns } = createRenderFns(template)
+      options.render = render
+      options.staticRenderFns = staticRenderFns
     }
   }
   mount.call(this, el)