Browse Source

expression cache

expressions with the same signature are now cached! this dramatically
improves performance on large v-repeat lists with multiple expressions.
Evan You 12 years ago
parent
commit
15a673388e
2 changed files with 14 additions and 3 deletions
  1. 6 1
      src/compiler.js
  2. 8 2
      src/directives/repeat.js

+ 6 - 1
src/compiler.js

@@ -63,6 +63,7 @@ function Compiler (vm, options) {
     // set compiler properties
     // set compiler properties
     compiler.vm = el.vue_vm = vm
     compiler.vm = el.vue_vm = vm
     compiler.bindings = makeHash()
     compiler.bindings = makeHash()
+    compiler.expCache = compiler.expCache || makeHash()
     compiler.dirs = []
     compiler.dirs = []
     compiler.deferred = []
     compiler.deferred = []
     compiler.computed = []
     compiler.computed = []
@@ -677,7 +678,11 @@ CompilerProto.defineMeta = function (key, binding) {
  */
  */
 CompilerProto.defineExp = function (key, binding, directive) {
 CompilerProto.defineExp = function (key, binding, directive) {
     var filters = directive && directive.computeFilters && directive.filters,
     var filters = directive && directive.computeFilters && directive.filters,
-        getter  = ExpParser.parse(key, this, null, filters)
+        exp     = filters ? directive.expression : key,
+        getter  = this.expCache[exp]
+    if (!getter) {
+        getter = this.expCache[exp] = ExpParser.parse(key, this, null, filters)
+    }
     if (getter) {
     if (getter) {
         this.markComputed(binding, getter)
         this.markComputed(binding, getter)
     }
     }

+ 8 - 2
src/directives/repeat.js

@@ -7,6 +7,10 @@ module.exports = {
 
 
         this.identifier = '$r' + this.id
         this.identifier = '$r' + this.id
 
 
+        // a hash to cache the same expressions on repeated instances
+        // so they don't have to be compiled for every single instance
+        this.expCache = utils.hash()
+
         var el   = this.el,
         var el   = this.el,
             ctn  = this.container = el.parentNode
             ctn  = this.container = el.parentNode
 
 
@@ -64,7 +68,8 @@ module.exports = {
             el     : el,
             el     : el,
             parent : this.vm,
             parent : this.vm,
             compilerOptions: {
             compilerOptions: {
-                repeat: true
+                repeat: true,
+                expCache: this.expCache
             }
             }
         }).$destroy()
         }).$destroy()
         this.initiated = true
         this.initiated = true
@@ -210,7 +215,8 @@ module.exports = {
                 data: data,
                 data: data,
                 parent: this.vm,
                 parent: this.vm,
                 compilerOptions: {
                 compilerOptions: {
-                    repeat: true
+                    repeat: true,
+                    expCache: this.expCache
                 }
                 }
             })
             })