From 13be50a4a9e34d65fef834625f44545e849fed02 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 26 Feb 2017 05:58:26 +0800 Subject: faster tree transversal (#1462) - convert `[].forEach()` to for-loops --- lib/ast.js | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'lib/ast.js') diff --git a/lib/ast.js b/lib/ast.js index 61643aed..f3df78fe 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -145,12 +145,13 @@ var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", { }, AST_Statement); function walk_body(node, visitor) { - if (node.body instanceof AST_Statement) { - node.body._walk(visitor); + var body = node.body; + if (body instanceof AST_Statement) { + body._walk(visitor); + } + else for (var i = 0, len = body.length; i < len; i++) { + body[i]._walk(visitor); } - else node.body.forEach(function(stat){ - stat._walk(visitor); - }); }; var AST_Block = DEFNODE("Block", "body", { @@ -371,9 +372,10 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", { _walk: function(visitor) { return visitor._visit(this, function(){ if (this.name) this.name._walk(visitor); - this.argnames.forEach(function(arg){ - arg._walk(visitor); - }); + var argnames = this.argnames; + for (var i = 0, len = argnames.length; i < len; i++) { + argnames[i]._walk(visitor); + } walk_body(this, visitor); }); } @@ -533,9 +535,10 @@ var AST_Definitions = DEFNODE("Definitions", "definitions", { }, _walk: function(visitor) { return visitor._visit(this, function(){ - this.definitions.forEach(function(def){ - def._walk(visitor); - }); + var definitions = this.definitions; + for (var i = 0, len = definitions.length; i < len; i++) { + definitions[i]._walk(visitor); + } }); } }, AST_Statement); @@ -573,9 +576,10 @@ var AST_Call = DEFNODE("Call", "expression args", { _walk: function(visitor) { return visitor._visit(this, function(){ this.expression._walk(visitor); - this.args.forEach(function(arg){ - arg._walk(visitor); - }); + var args = this.args; + for (var i = 0, len = args.length; i < len; i++) { + args[i]._walk(visitor); + } }); } }); @@ -742,9 +746,10 @@ var AST_Array = DEFNODE("Array", "elements", { }, _walk: function(visitor) { return visitor._visit(this, function(){ - this.elements.forEach(function(el){ - el._walk(visitor); - }); + var elements = this.elements; + for (var i = 0, len = elements.length; i < len; i++) { + elements[i]._walk(visitor); + } }); } }); @@ -756,9 +761,10 @@ var AST_Object = DEFNODE("Object", "properties", { }, _walk: function(visitor) { return visitor._visit(this, function(){ - this.properties.forEach(function(prop){ - prop._walk(visitor); - }); + var properties = this.properties; + for (var i = 0, len = properties.length; i < len; i++) { + properties[i]._walk(visitor); + } }); } }); -- cgit v1.2.3