-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize simple classes in private scope #3679
Comments
That does not work in the general case - for instance: |
Actually I do not see reason (except for the possible complexity or by the scope where it was declared) to the output not be optimized to something like: console.log('function'); // or
console.log(typeof Function); Step-by-step: function f() {
function f() {} // f() is declared
function g() {
return this;
}
f.g = g; // Unique f() property
return f.g(); // Unique usage of f.g property
// So f() could be dropped to use g directly.
}
console.log(typeof f()); function f() {
function g() {
return this;
}
return g(); // g() will return this (from a very simple Function, that in this
// scope have the same construction of the external f()). So we can
// replace it direcly with this (due the next unique usage).
}
console.log(typeof f()); function f() {
return this;
}
console.log(typeof f()); // f() will return this (that is a Function),
// so it could be modified. console.log(typeof Function); // typeof Function === 'function' console.log('function'); I am considering here that optimization can take into account how functions and variables are exposed to the global scope, and that it is possible to identify how many times a given function is used and its possible return. So for instance (considering that the above steps are possible): function f() {
function f() {}
function g() {
return this;
}
f.g = g;
return f.g();
}
console.log(typeof f());
console.log(typeof f());
console.log(typeof f()); Could be safety optimized to: console.log('function');
console.log('function');
console.log('function'); Although |
This doesn't do what you think it does: function f() {
return this;
}
console.log(typeof f()); $ echo function f(){return this} console.log(typeof f()) | node
object So it certainly isn't a valid optimisation step. |
Really, but anyway, it could be optimized to |
Well, it couldn't since that will change the result of the output which violates the basic assumption of operation. I've put together a Pull Request that will work on your original example − feel free to improve upon it. |
Perfect! |
Bug report or feature request? Feature request
Uglify version (
uglifyjs -V
) 3.7.4JavaScript input
The
uglifyjs
CLI command executed orminify()
options used.JavaScript output or error produced.
Expected:
Note that
Test
class have only a single method, that is called statically a single time. It is too private, so it can't accessed by external scope.A simplest example:
The text was updated successfully, but these errors were encountered: