Skip to content

Commit 315cfe8

Browse files
author
Evan You
committed
Directive parsing performance improvement
- test for existence of separators first before matching expensive RegExps
1 parent 2730940 commit 315cfe8

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/directive.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,26 @@ var DirProto = Directive.prototype
7777
*/
7878
function parseKey (dir, rawKey) {
7979

80-
var argMatch = rawKey.match(ARG_RE)
81-
82-
var key = argMatch
83-
? argMatch[2].trim()
84-
: rawKey.trim()
85-
86-
dir.arg = argMatch
87-
? argMatch[1].trim()
88-
: null
80+
var key = rawKey
81+
if (rawKey.indexOf(':') > -1) {
82+
var argMatch = rawKey.match(ARG_RE)
83+
key = argMatch
84+
? argMatch[2].trim()
85+
: key
86+
dir.arg = argMatch
87+
? argMatch[1].trim()
88+
: null
89+
}
8990

90-
var nesting = key.match(NESTING_RE)
91-
dir.nesting = nesting
92-
? nesting[0].length
91+
// nesting
92+
var firstChar = key.charAt(0)
93+
dir.root = firstChar === '*'
94+
dir.nesting = firstChar === '^'
95+
? key.match(NESTING_RE)[0].length
9396
: false
9497

95-
dir.root = key.charAt(0) === '*'
96-
9798
if (dir.nesting) {
98-
key = key.replace(NESTING_RE, '')
99+
key = key.slice(dir.nesting)
99100
} else if (dir.root) {
100101
key = key.slice(1)
101102
}
@@ -204,7 +205,9 @@ DirProto.unbind = function (update) {
204205
* multiple clauses
205206
*/
206207
Directive.split = function (exp) {
207-
return exp.match(SPLIT_RE) || ['']
208+
return exp.indexOf(',') > -1
209+
? exp.match(SPLIT_RE) || ['']
210+
: [exp]
208211
}
209212

210213
/**
@@ -220,8 +223,16 @@ Directive.parse = function (dirname, expression, compiler, node) {
220223
var dir = compiler.getOption('directives', dirname) || directives[dirname]
221224
if (!dir) return utils.warn('unknown directive: ' + dirname)
222225

223-
var keyMatch = expression.match(KEY_RE),
224-
rawKey = keyMatch && keyMatch[0].trim()
226+
var rawKey
227+
if (expression.indexOf('|') > -1) {
228+
var keyMatch = expression.match(KEY_RE)
229+
if (keyMatch) {
230+
rawKey = keyMatch[0].trim()
231+
}
232+
} else {
233+
rawKey = expression.trim()
234+
}
235+
225236
// have a valid raw key, or be an empty directive
226237
return (rawKey || expression === '')
227238
? new Directive(dir, expression, rawKey, compiler, node)

0 commit comments

Comments
 (0)