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.
This commit is contained in:
Mihai Bazon
2012-09-17 12:24:21 +03:00
parent 14481de0e9
commit 92e22c460d

View File

@@ -91,6 +91,12 @@ function Compressor(options, false_by_default) {
self = p; self = p;
} }
}; };
function find_parent(type) {
for (var i = stack.length; --i >= 0;) {
var x = stack[i];
if (x instanceof type) return x;
}
};
return { return {
option : function(key) { return options[key] }, option : function(key) { return options[key] },
push_node : function(node) { stack.push(node) }, push_node : function(node) { stack.push(node) },
@@ -104,7 +110,8 @@ function Compressor(options, false_by_default) {
if (options.warnings) if (options.warnings)
AST_Node.warn.apply(AST_Node, arguments); 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){ AST_Undefined.DEFMETHOD("optimize", function(compressor){
// if (compressor.option("unsafe") && !(compressor.parent() instanceof AST_Array)) { if (compressor.option("unsafe")) {
// return make_node(AST_Sub, this, { var scope = compressor.find_parent(AST_Scope);
// expression: make_node(AST_Array, this, { var undef = scope.find_variable("undefined");
// elements: [] if (undef) {
// }), var ref = make_node(AST_SymbolRef, this, {
// property: make_node(AST_Number, this, { name : "undefined",
// value: 0 scope : scope,
// }) thedef : undef
// }); });
// } ref.reference();
return ref;
}
}
return this; return this;
}); });