Skip to content

Commit

Permalink
refactoring of match result getTrace() method (25% performance boost)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jul 12, 2017
1 parent 7f1842c commit 92705b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions lib/lexer/trace.js
Expand Up @@ -2,41 +2,42 @@ function getTrace(node) {
function hasMatch(matchNode) {
if (matchNode.type === 'ASTNode') {
if (matchNode.node === node) {
result = [];
return true;
}

if (matchNode.childrenMatch) {
// use for-loop for better perfomance
for (var i = 0; i < matchNode.childrenMatch.length; i++) {
if (hasMatch(matchNode.childrenMatch[i])) {
return true;
}
}
}
} else {
var addToStack = matchNode.syntax.type === 'Type' ||
matchNode.syntax.type === 'Property' ||
matchNode.syntax.type === 'Keyword';

if (addToStack) {
stack.push(matchNode.syntax);
}

// use for-loop for better perfomance
for (var i = 0; i < matchNode.match.length; i++) {
if (hasMatch(matchNode.match[i], node, stack)) {
if (hasMatch(matchNode.match[i])) {
if (matchNode.syntax.type === 'Type' ||
matchNode.syntax.type === 'Property' ||
matchNode.syntax.type === 'Keyword') {
result.unshift(matchNode.syntax);
}
return true;
}
}

if (addToStack) {
stack.pop();
}
}

return false;
}

var stack = [];
return this.matched !== null && hasMatch(this.matched) ? stack : null;
var result = null;

if (this.matched !== null) {
hasMatch(this.matched);
}

return result;
}

function testNode(match, node, fn) {
Expand Down
2 changes: 1 addition & 1 deletion test/lexer.js
Expand Up @@ -267,7 +267,7 @@ describe('lexer', function() {
var match = syntax.lexer.matchProperty('background', ast);
var mismatch = syntax.lexer.matchProperty('margin', ast);

it('getNodeTrace', function() {
it('getTrace', function() {
assert.deepEqual(match.getTrace(testNode), [
{ type: 'Type', name: 'final-bg-layer' },
{ type: 'Property', name: 'background-color' },
Expand Down

0 comments on commit 92705b4

Please sign in to comment.