summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Parser.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-14 02:51:16 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-14 02:51:16 +0200
commit910de95e7aadcbdb8a3f04ec1ec4c6518ef1cd86 (patch)
tree7c655734b0111902843da9bc4250ad050e95f3be /Userland/Libraries/LibJS/Parser.cpp
parenta50e33abe3a34aba965e329a615eab4d87227e4c (diff)
downloadserenity-910de95e7aadcbdb8a3f04ec1ec4c6518ef1cd86.zip
LibJS: Add a fast failure path to try_parse_arrow_function_expression()
The save/load of parser state performed by lookahead parsing is quite expensive so let's try to avoid it in the most common case. This is a 15-20% speedup on various chunks of JS I've tested. :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Parser.cpp')
-rw-r--r--Userland/Libraries/LibJS/Parser.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp
index 72d8d8f94f..b4c9d76693 100644
--- a/Userland/Libraries/LibJS/Parser.cpp
+++ b/Userland/Libraries/LibJS/Parser.cpp
@@ -449,6 +449,20 @@ static bool is_simple_parameter_list(Vector<FunctionNode::Parameter> const& para
RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expect_parens)
{
+ if (!expect_parens) {
+ // NOTE: This is a fast path where we try to fail early in case this can't possibly
+ // be a match. The idea is to avoid the expensive parser state save/load mechanism.
+ // The logic is duplicated below in the "real" !expect_parens branch.
+ if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await))
+ return nullptr;
+ auto forked_lexer = m_state.lexer;
+ auto token = forked_lexer.next();
+ if (token.trivia_contains_line_terminator())
+ return nullptr;
+ if (token.type() != TokenType::Arrow)
+ return nullptr;
+ }
+
save_state();
auto rule_start = push_start();