diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-09-17 12:24:21 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-09-17 12:27:32 +0300 |
commit | 92e22c460d689008055f4f3f9b850989e4359f4d (patch) | |
tree | 5d56c00bd2ad266431c687d59daac45ec39dec89 /lib | |
parent | 14481de0e9141eff41877f56876f7e6a78c3d059 (diff) | |
download | tracifyjs-92e22c460d689008055f4f3f9b850989e4359f4d.tar.gz tracifyjs-92e22c460d689008055f4f3f9b850989e4359f4d.zip |
possible optimization for AST_Undefined
if undefined is defined, ;-), we replace AST_Undefined nodes to a reference
to the "undefined" variable; in turn the mangler will compress it to a
single letter; this helps at least on jQuery.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/lib/compress.js b/lib/compress.js index 7b865fdd..ddbf41fa 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -91,6 +91,12 @@ function Compressor(options, false_by_default) { self = p; } }; + function find_parent(type) { + for (var i = stack.length; --i >= 0;) { + var x = stack[i]; + if (x instanceof type) return x; + } + }; return { option : function(key) { return options[key] }, push_node : function(node) { stack.push(node) }, @@ -104,7 +110,8 @@ function Compressor(options, false_by_default) { if (options.warnings) AST_Node.warn.apply(AST_Node, arguments); }, - in_boolean_context: in_boolean_context + in_boolean_context: in_boolean_context, + find_parent: find_parent, }; }; @@ -1499,16 +1506,19 @@ function Compressor(options, false_by_default) { }); AST_Undefined.DEFMETHOD("optimize", function(compressor){ - // if (compressor.option("unsafe") && !(compressor.parent() instanceof AST_Array)) { - // return make_node(AST_Sub, this, { - // expression: make_node(AST_Array, this, { - // elements: [] - // }), - // property: make_node(AST_Number, this, { - // value: 0 - // }) - // }); - // } + if (compressor.option("unsafe")) { + var scope = compressor.find_parent(AST_Scope); + var undef = scope.find_variable("undefined"); + if (undef) { + var ref = make_node(AST_SymbolRef, this, { + name : "undefined", + scope : scope, + thedef : undef + }); + ref.reference(); + return ref; + } + } return this; }); |