Added regex instance caching to resolver

This commit is contained in:
Jim Cowart 2013-03-03 00:25:48 -05:00
parent 2097c638ab
commit 3f1396cb35
3 changed files with 38 additions and 39 deletions

View file

@ -203,27 +203,29 @@
}
};
var bindingsResolver = {
cache : { },
cache : {},
regex : {},
compare : function ( binding, topic ) {
var result = (this.cache[topic] && this.cache[topic][binding]);
var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]);
if(typeof result !== "undefined") {
return result;
}
var prev;
var pattern = "^" + _.map(binding.split('.'), function(segment) {
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
if(segment === "#") {
res += "[A-Z,a-z,0-9,\\.]*"
} else if (segment === "*") {
res += "[A-Z,a-z,0-9]+"
} else {
res += segment;
}
prev = segment;
return res;
} ).join('') + "$";
var rgx = new RegExp( pattern );
if(!(rgx = this.regex[binding])) {
pattern = "^" + _.map(binding.split('.'), function(segment) {
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
if(segment === "#") {
res += "[A-Z,a-z,0-9,\\.]*"
} else if (segment === "*") {
res += "[A-Z,a-z,0-9]+"
} else {
res += segment;
}
prev = segment;
return res;
} ).join('') + "$";
rgx = this.regex[binding] = new RegExp( pattern );
}
this.cache[topic] = this.cache[topic] || {};
this.cache[topic][binding] = result = rgx.test( topic );
return result;

8
lib/postal.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,25 +1,27 @@
var bindingsResolver = {
cache : { },
cache : {},
regex : {},
compare : function ( binding, topic ) {
var result = (this.cache[topic] && this.cache[topic][binding]);
var pattern, rgx, prev, result = (this.cache[topic] && this.cache[topic][binding]);
if(typeof result !== "undefined") {
return result;
}
var prev;
var pattern = "^" + _.map(binding.split('.'), function(segment) {
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
if(segment === "#") {
res += "[A-Z,a-z,0-9,\\.]*"
} else if (segment === "*") {
res += "[A-Z,a-z,0-9]+"
} else {
res += segment;
}
prev = segment;
return res;
} ).join('') + "$";
var rgx = new RegExp( pattern );
if(!(rgx = this.regex[binding])) {
pattern = "^" + _.map(binding.split('.'), function(segment) {
var res = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
if(segment === "#") {
res += "[A-Z,a-z,0-9,\\.]*"
} else if (segment === "*") {
res += "[A-Z,a-z,0-9]+"
} else {
res += segment;
}
prev = segment;
return res;
} ).join('') + "$";
rgx = this.regex[binding] = new RegExp( pattern );
}
this.cache[topic] = this.cache[topic] || {};
this.cache[topic][binding] = result = rgx.test( topic );
return result;
@ -27,5 +29,6 @@ var bindingsResolver = {
reset : function () {
this.cache = {};
this.regex = {};
}
};