From 24bfd55a22afd791d4a97694641831cfbd27fb14 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Sun, 2 Sep 2012 11:11:39 +0300 Subject: minor perf. improvements --- lib/parse.js | 46 +++++++++++++++++++++++----------------------- lib/utils.js | 13 ++++++++++--- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 416c363a..d6893253 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -748,7 +748,7 @@ function parse($TEXT, exigent_mode) { function parenthesised() { expect("("); - var exp = expression(); + var exp = expression(true); expect(")"); return exp; }; @@ -756,7 +756,7 @@ function parse($TEXT, exigent_mode) { function embed_tokens(parser) { return function() { var start = S.token; - var expr = parser.apply(this, arguments); + var expr = parser(); var end = prev(); expr.start = start; expr.end = end; @@ -933,9 +933,9 @@ function parse($TEXT, exigent_mode) { function regular_for(init) { expect(";"); - var test = is("punc", ";") ? null : expression(); + var test = is("punc", ";") ? null : expression(true); expect(";"); - var step = is("punc", ")") ? null : expression(); + var step = is("punc", ")") ? null : expression(true); expect(")"); return new AST_For({ init : init, @@ -947,7 +947,7 @@ function parse($TEXT, exigent_mode) { function for_in(init) { var lhs = init instanceof AST_Var ? init.definitions[0].name : null; - var obj = expression(); + var obj = expression(true); expect(")"); return new AST_ForIn({ init : init, @@ -957,6 +957,20 @@ function parse($TEXT, exigent_mode) { }); }; + var function_body = embed_tokens(function(){ + ++S.in_function; + var loop = S.in_loop; + var labels = S.labels; + S.in_directives = true; + S.in_loop = 0; + S.labels = []; + var a = block_(true); + --S.in_function; + S.in_loop = loop; + S.labels = labels; + return a; + }); + var function_ = function(in_statement) { var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun @@ -975,19 +989,7 @@ function parse($TEXT, exigent_mode) { next(); return a; })(true, []), - body: embed_tokens(function(){ - ++S.in_function; - var loop = S.in_loop; - var labels = S.labels; - S.in_directives = true; - S.in_loop = 0; - S.labels = []; - var a = block_(true); - --S.in_function; - S.in_loop = loop; - S.labels = labels; - return a; - })() + body: function_body() }); }; @@ -1032,7 +1034,7 @@ function parse($TEXT, exigent_mode) { cur = []; branch = new AST_Case({ start : prog1(S.token, next), - expression : expression(), + expression : expression(true), body : cur }); a.push(branch); @@ -1187,7 +1189,7 @@ function parse($TEXT, exigent_mode) { switch (start.value) { case "(": next(); - var ex = expression(); + var ex = expression(true); ex.start = start; ex.end = S.token; expect(")"); @@ -1331,7 +1333,7 @@ function parse($TEXT, exigent_mode) { } if (is("punc", "[")) { next(); - var prop = expression(); + var prop = expression(true); expect("]"); return subscripts(new AST_Sub({ start : start, @@ -1451,8 +1453,6 @@ function parse($TEXT, exigent_mode) { }; var expression = function(commas, no_in) { - if (arguments.length == 0) - commas = true; var start = S.token; var expr = maybe_assign(no_in); if (commas && is("punc", ",")) { diff --git a/lib/utils.js b/lib/utils.js index dbd0079f..12194293 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -46,11 +46,12 @@ function curry(f) { return function() { return f.apply(this, args.concat(slice(arguments))); }; }; -function prog1(ret) { +function prog1(ret, f1, f2, f3) { if (ret instanceof Function) ret = ret(); - for (var i = 1, n = arguments.length; --n > 0; ++i) - arguments[i](); + if (f1) f1(); + if (f2) f2(); + if (f3) f3(); return ret; }; @@ -169,3 +170,9 @@ function string_template(text, props) { return props[p]; }); }; + +function time_it(name, cont) { + var t1 = new Date().getTime(); + try { return cont(); } + finally { sys.debug("// " + name + ": " + ((new Date().getTime() - t1) / 1000).toFixed(3) + " sec."); } +}; -- cgit v1.2.3