Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f6fe33

Browse files
committed
performance improvement of single statement in parser
1 parent c020867 commit 7f6fe33

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/parser.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,19 @@ function parser(text, json){
325325
if (tokens.length > 0 && !peek('}', ')', ';', ']'))
326326
statements.push(filterChain());
327327
if (!expect(';')) {
328-
return function (self){
329-
var value;
330-
for ( var i = 0; i < statements.length; i++) {
331-
var statement = statements[i];
332-
if (statement)
333-
value = statement(self);
334-
}
335-
return value;
336-
};
328+
// optimize for the common case where there is only one statement.
329+
// TODO(size): maybe we should not support multiple statements?
330+
return statements.length == 1
331+
? statements[0]
332+
: function (self){
333+
var value;
334+
for ( var i = 0; i < statements.length; i++) {
335+
var statement = statements[i];
336+
if (statement)
337+
value = statement(self);
338+
}
339+
return value;
340+
};
337341
}
338342
}
339343
}

0 commit comments

Comments
 (0)