Przeglądaj źródła

cache linker for v-ifs

Evan You 11 lat temu
rodzic
commit
9f37925602
2 zmienionych plików z 13 dodań i 6 usunięć
  1. 1 1
      src/cache.js
  2. 12 5
      src/directives/if.js

+ 1 - 1
src/cache.js

@@ -15,7 +15,7 @@ function Cache (limit) {
   this.size = 0
   this.limit = limit
   this.head = this.tail = undefined
-  this._keymap = {}
+  this._keymap = Object.create(null)
 }
 
 var p = Cache.prototype

+ 12 - 5
src/directives/if.js

@@ -2,6 +2,8 @@ var _ = require('../util')
 var compiler = require('../compiler')
 var templateParser = require('../parsers/template')
 var transition = require('../transition')
+var Cache = require('../cache')
+var cache = new Cache(1000)
 
 module.exports = {
 
@@ -19,11 +21,16 @@ module.exports = {
         this.template.appendChild(templateParser.clone(el))
       }
       // compile the nested partial
-      this.linker = compiler.compile(
-        this.template,
-        this.vm.$options,
-        true
-      )
+      var cacheId = (this.vm.constructor.cid || '') + el.outerHTML
+      this.linker = cache.get(cacheId)
+      if (!this.linker) {
+        this.linker = compiler.compile(
+          this.template,
+          this.vm.$options,
+          true
+        )
+        cache.put(cacheId, this.linker)
+      }
     } else {
       process.env.NODE_ENV !== 'production' && _.warn(
         'v-if="' + this.expression + '" cannot be ' +