فهرست منبع

use Object.create(null) for better hash

Evan You 12 سال پیش
والد
کامیت
91d852863c
7فایلهای تغییر یافته به همراه28 افزوده شده و 15 حذف شده
  1. 9 7
      src/compiler.js
  2. 1 1
      src/deps-parser.js
  3. 2 1
      src/directives/repeat.js
  4. 1 1
      src/exp-parser.js
  5. 1 1
      src/main.js
  6. 1 1
      src/observer.js
  7. 13 3
      src/utils.js

+ 9 - 7
src/compiler.js

@@ -12,6 +12,8 @@ var Emitter     = require('./emitter'),
     slice       = Array.prototype.slice,
     log         = utils.log,
     def         = utils.defProtected,
+    makeHash    = utils.hash,
+    hasOwn      = Object.prototype.hasOwnProperty,
 
     // special directives
     idAttr,
@@ -33,7 +35,7 @@ function Compiler (vm, options) {
     var compiler = this
 
     // extend options
-    options = compiler.options = options || {}
+    options = compiler.options = options || makeHash()
     utils.extend(compiler, options.compilerOptions)
 
     // initialize element
@@ -46,7 +48,7 @@ function Compiler (vm, options) {
 
     compiler.vm  = vm
     // special VM properties are inumerable
-    def(vm, '$', {})
+    def(vm, '$', makeHash())
     def(vm, '$el', compiler.el)
     def(vm, '$compiler', compiler)
 
@@ -67,7 +69,7 @@ function Compiler (vm, options) {
     var parent = compiler.parentCompiler
     compiler.bindings = parent
         ? Object.create(parent.bindings)
-        : {}
+        : makeHash()
     compiler.rootCompiler = parent
         ? getRoot(parent)
         : compiler
@@ -168,7 +170,7 @@ CompilerProto.setupObserver = function () {
 
     // a hash to hold event proxies for each root level key
     // so they can be referenced and removed later
-    observer.proxies = {}
+    observer.proxies = makeHash()
 
     // add own listeners which trigger binding updates
     observer
@@ -337,7 +339,7 @@ CompilerProto.bindDirective = function (directive) {
         // If the directive's owner compiler's VM has the key,
         // it belongs there. Create the binding if it's not already
         // created, and return it.
-        binding = ownerCompiler.bindings.hasOwnProperty(key)
+        binding = hasOwn.call(ownerCompiler.bindings, key)
             ? ownerCompiler.bindings[key]
             : ownerCompiler.createBinding(key)
     } else {
@@ -417,7 +419,7 @@ CompilerProto.createBinding = function (key, isExp) {
             compiler.define(key, binding)
         } else {
             var parentKey = key.slice(0, key.lastIndexOf('.'))
-            if (!bindings.hasOwnProperty(parentKey)) {
+            if (!hasOwn.call(bindings, parentKey)) {
                 // this is a nested value binding, but the binding for its parent
                 // has not been created yet. We better create that one too.
                 compiler.createBinding(parentKey)
@@ -578,7 +580,7 @@ CompilerProto.destroy = function () {
     }
     // unbind/unobserve all own bindings
     for (key in bindings) {
-        if (bindings.hasOwnProperty(key)) {
+        if (hasOwn.call(bindings, key)) {
             binding = bindings[key]
             if (binding.root) {
                 Observer.unobserve(binding.value, binding.key, compiler.observer)

+ 1 - 1
src/deps-parser.js

@@ -8,7 +8,7 @@ var Emitter  = require('./emitter'),
  */
 function catchDeps (binding) {
     utils.log('\n─ ' + binding.key)
-    var depsHash = {}
+    var depsHash = utils.hash()
     observer.on('get', function (dep) {
         if (depsHash[dep.key]) return
         depsHash[dep.key] = 1

+ 2 - 1
src/directives/repeat.js

@@ -1,6 +1,7 @@
 var config   = require('../config'),
     Observer = require('../observer'),
     Emitter  = require('../emitter'),
+    utils    = require('../utils'),
     ViewModel // lazy def to avoid circular dependency
 
 /**
@@ -107,7 +108,7 @@ module.exports = {
 
         this.unbind(true)
         // attach an object to container to hold handlers
-        this.container.sd_dHandlers = {}
+        this.container.sd_dHandlers = utils.hash()
         // if initiating with an empty collection, we need to
         // force a compile so that we get all the bindings for
         // dependency extraction.

+ 1 - 1
src/exp-parser.js

@@ -58,7 +58,7 @@ module.exports = {
         var args = [],
             v, i, keyPrefix,
             l = vars.length,
-            hash = {}
+            hash = Object.create(null)
         for (i = 0; i < l; i++) {
             v = vars[i]
             // avoid duplicate keys

+ 1 - 1
src/main.js

@@ -110,7 +110,7 @@ function extend (options) {
  *  extension option, but only as an instance option.
  */
 function inheritOptions (child, parent, topLevel) {
-    child = child || {}
+    child = child || utils.hash()
     convertPartials(child.partials)
     if (!parent) return child
     for (var key in parent) {

+ 1 - 1
src/observer.js

@@ -171,7 +171,7 @@ module.exports = {
                 def(obj, '__observer__', new Emitter())
             }
             ob = obj.__observer__
-            ob.values = ob.values || {}
+            ob.values = ob.values || utils.hash()
             var proxies = observer.proxies[path] = {
                 get: function (key) {
                     observer.emit('get', path + key)

+ 13 - 3
src/utils.js

@@ -3,13 +3,23 @@ var config    = require('./config'),
     join      = Array.prototype.join,
     console   = window.console
 
+/**
+ *  Create a prototype-less object
+ *  which is a better hash/map
+ */
+function makeHash () {
+    return Object.create(null)
+}
+
 module.exports = {
 
+    hash: makeHash,
+
     // global storage for user-registered
     // vms, partials and transitions
-    viewmodels  : {},
-    partials    : {},
-    transitions : {},
+    viewmodels  : makeHash(),
+    partials    : makeHash(),
+    transitions : makeHash(),
 
     /**
      *  Define an ienumerable property