Skip to content

Commit

Permalink
optimised regexps to create them during compile time, reuse regexp in…
Browse files Browse the repository at this point in the history
…stances
  • Loading branch information
epoberezkin committed Aug 22, 2015
1 parent 14c6426 commit 843557e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
24 changes: 23 additions & 1 deletion lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function compile(schema, root, localRefs) {
/* jshint validthis: true, evil: true */
var self = this
, refVal = [ undefined ]
, refs = {};
, refs = {}
, patterns = []
, patternsHash = {};

root = root || { schema: schema, refVal: refVal, refs: refs };

Expand All @@ -42,10 +44,13 @@ function compile(schema, root, localRefs) {
util: util,
resolve: resolve,
resolveRef: resolveRef,
usePattern: usePattern,
opts: self.opts,
formats: formats
});

validateCode = patternsCode(patterns) + validateCode;

if (self.opts.beautify) {
var opts = self.opts.beautify === true ? { indent_size: 2 } : self.opts.beautify;
if (beautify) validateCode = beautify(validateCode, opts);
Expand Down Expand Up @@ -103,6 +108,23 @@ function compile(schema, root, localRefs) {
var refId = refs[ref];
refVal[refId] = v;
}

function usePattern(regexStr) {
var index = patternsHash[regexStr];
if (index === undefined) {
index = patternsHash[regexStr] = patterns.length;
patterns[index] = regexStr;
}
return 'pattern' + index;
}

function patternsCode(patterns) {
if (!patterns.length) return '';
var code = '';
for (var i=0; i<patterns.length; i++)
code += 'var pattern' + i + ' = new RegExp(' + util.toQuotedString(patterns[i]) + ');'
return code;
}
}


Expand Down
7 changes: 0 additions & 7 deletions lib/compile/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
toHash: toHash,
getProperty: getProperty,
escapeQuotes: escapeQuotes,
escapeRegExp: escapeRegExp,
ucs2length: ucs2length,
varOccurences: varOccurences,
varReplace: varReplace,
Expand Down Expand Up @@ -95,12 +94,6 @@ function escapeQuotes(str) {
}


var ESCAPE_REGEXP = /[\/]/g;
function escapeRegExp(str) {
return str.replace(ESCAPE_REGEXP, '\\$&');
}


// https://mathiasbynens.be/notes/javascript-encoding
// https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
function ucs2length(str) {
Expand Down
2 changes: 1 addition & 1 deletion lib/dot/pattern.jst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{{# def.setup:'pattern' }}

{{ new RegExp($schema); /* test if regexp is valid to fail at compile time rather than in eval */}}
if (! /{{= it.util.escapeRegExp($schema) }}/.test({{=$data}}) ) {
if (! {{= it.usePattern($schema) }}.test({{=$data}}) ) {
{{# def.error:'pattern' }}
} {{? $breakOnError }} else { {{?}}
4 changes: 2 additions & 2 deletions lib/dot/properties.jst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var valid{{=$it.level}} = true;
{{? $pPropertyKeys.length }}
if (isAdditional{{=$lvl}}) {
{{~ $pPropertyKeys:$pProperty:$i }}
if (/{{= it.util.escapeRegExp($pProperty) }}/.test(key{{=$lvl}}))
if ({{= it.usePattern($pProperty) }}.test(key{{=$lvl}}))
isAdditional{{=$lvl}} = false;
{{? $i < $pPropertyKeys.length-1 }}
else
Expand Down Expand Up @@ -140,7 +140,7 @@ var valid{{=$it.level}} = true;
}}

for (var key{{=$lvl}} in {{=$data}}) {
if (/{{= it.util.escapeRegExp($pProperty) }}/.test(key{{=$lvl}})) {
if ({{= it.usePattern($pProperty) }}.test(key{{=$lvl}})) {
{{
$it.errorPath = it.util.getPathExpr(it.errorPath, 'key' + $lvl, it.opts.jsonPointers);
var $passData = $data + '[key' + $lvl + ']';
Expand Down

0 comments on commit 843557e

Please sign in to comment.