You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is one of those almost-safe optimizations which will "work" most of the time but could silently bite you. We'll need to have it as part of the advanced/unsafe category.
This is also somewhat similar to Number(...) → +... transformation that we already do — it's correct unless native method was tampered with (which we can't check).
In practice, I'm seeing tons of stuff like __d("...",[],function(b,c,d,e,f,g){c.__markCompiled&&c.__markCompiled() /* stuff that doesn't use e,f,g */ },null); in our code and it's most certainly safe to strip those args, saving a lot of unnecessary boilerplate.
Maybe have it as an option. FWIW GCC does this by default.
On Wed, Jun 1, 2016 at 5:39 PM Juriy Zaytsev [email protected]
wrote:
Yep.
This is one of those almost-safe optimizations which will "work" most of
the time but could silently bite you. We'll need to have it as part of the
advanced/unsafe category.
This is also somewhat similar to Number(...) → +... transformation that
we already do — it's correct unless native method was tampered with (which
we can't check).
In practice, I'm seeing tons of stuff like __d("...",[],function(b,c,d,e,f,g){c.__markCompiled&&c.__markCompiled()
/* stuff that doesn't use e,f,g */ },null); in our code and it's most
certainly safe to strip those args, saving a lot of unnecessary boilerplate.
(function(){functionon(){}functionoff(){}module.exports=on;module.exports.on=on;module.exports.off=off;}());uglify!function(){functiono(){}functionn(){}module.exports=o,module.exports.on=o,module.exports.off=n}();babel(function(){functiona(){}module.exports=a,module.exports.on=a,module.exports.off=function(){}})();closure(function(){functiona(){}module.exports=a;module.exports.on=a;module.exports.off=function(){}})();(ideal)(function(){functiona(){}module.exports.on=module.exports=a;module.exports.off=function(){}})();// this transform(next)(function(){module.exports.on=module.exports=function(){},module.exports.off=function(){}})();// would allow for this(next)module.exports.on=module.exports=function(){},module.exports.off=function(){};// and then this
This was a wrong repeat implementation, but look at that output
(function(){functionrepeat(text,repeat){for(vari=0;i<repeat;i++){text+=text;}returnrepeat;// mistake here, it should return text}foo=repeat('hello',1)}());
closure output: (function(){for(vara=0;1>a;a++);foo=1})();
Notes:foo is resolved. The loop is truncated, but it's still there.
We can't turn function functions into arrow functions. It's an unsafe optimisation. Arrow functions have no prototype and cannot be used as constructors so they aren't the same.
Reacted by fregante, Boopathi Rajaa and Sergey Rubanov
James: I don't think that's necessary because you can always add the
plugins you need before the minify step
On Wed, Jul 6, 2016, 2:26 PM Sebastian McKenzie [email protected]
wrote:
We can't turn function functions into arrow functions. It's an unsafe
optimisation. Arrow functions have no prototype and cannot be used as
constructors.
Yes, but there are places where ES5 code could be optimized more by using ES6+ features. I'm sure there are cases where we'd find arrow functions acceptable to use. Should optimizations like that be inferred? configured?
@thejameskyle Should optimizations like that be inferred?
Ideally the user would either be able to enable/disable each transform or use browserlist to figure out which ES6 transforms are safe. But maybe a target:"es6" could work.
@kittens then maybe the function-to-arrow transform could only apply to functions that are verified to never user the prototype.
It may be early for this, but should babel-minify have a repo dedicated to transforms/plugin suggestions? One-suggestion-per-issue would make it easier to discuss, track and "rate" each transform (difficulty vs. how common it is vs. savings)
For the 1st case, I guess we would need to "fold" all the same-named methods as long as they're the only ones present in blocks and are in the same order?
if(x){foo(a);bar(y);}else{foo(b);bar(z);}// becomesfoo(x ? a : b);bar(x ? y : z);
@hzoo Would it make sense to open a repo for the suggestions as suggested earlier? Or maybe just one-enhancement-per-issue so they can be easily discussed and tracked.
That'd be great! Anything that isn't used more than once should be inlined. I often declare variables for readability but I could just as well write one-liners instead.
Just like how Babel creates a _classCallCheck helper variable that makes sure new is used when constructing ES6 classes, Babili could create a _babiliUndefined variable that could get mangled down to a single character, which is significantly shorter than void 0. By taking advantage of the same mechanism that Babel uses to generate a non-conflicting variable name, we could avoid problems when people name a variable _babiliUndefined. Example:
if(foo===undefined){returnundefined}// becomesvar_babiliUndefinedif(foo===_babiliUndefined){return_babiliUndefined}// after manglingvaraif(b===a){returna}
Coming from #591, where @loganfsmyth suggested (after some discussion) that babel-minify place a _void0 or similar variable at the top, then replace all uses of undefined with it, allowing for mangling:
It should be possible to either put the var _void0 variable at the top of sourceType: module scripts or ones that are node modules. In other cases, they can be put at the top of function scopes, or the entire code could be wrapped in a block:
{let_void0// code here}
This would increase code size if void 0 only appeared once, but would save 5 characters if used twice and 11 if used thrice.
Activity
sebmck commentedon Jun 2, 2016
Removing unused arguments is an unsafe optimisation as it will effect runtime behaviour as
lengthwill be changed.kangax commentedon Jun 2, 2016
Yep.
This is one of those almost-safe optimizations which will "work" most of the time but could silently bite you. We'll need to have it as part of the advanced/unsafe category.
This is also somewhat similar to
Number(...)→+...transformation that we already do — it's correct unless native method was tampered with (which we can't check).In practice, I'm seeing tons of stuff like
__d("...",[],function(b,c,d,e,f,g){c.__markCompiled&&c.__markCompiled() /* stuff that doesn't use e,f,g */ },null);in our code and it's most certainly safe to strip those args, saving a lot of unnecessary boilerplate.amasad commentedon Jun 2, 2016
Maybe have it as an option. FWIW GCC does this by default.
On Wed, Jun 1, 2016 at 5:39 PM Juriy Zaytsev [email protected]
wrote:
fregante commentedon Jul 6, 2016
Edit 1: added more
Use arrow functions (in ES6 browsers)
Minify
Array()andObject()type constructors: Minify more type constructors #45 🎉Minify
RegExptype constructorUse assignment operators where possible
Merge object additions near object creation (
a={};a.a=1=>a={a:1})Don't assign or return
undefined(var a = undefinedandreturn undefined)Math.abscan be generatedUnsafe with
-0Resolve simple functions
Examples...
boopathi/babel-minifyhas someRemove unnecessary IIFE
Remove shadowed+unused variables/functions (unless they access properties)
Minify
Functiontype constructorUnsafe because
new Function's functions are created in the global scope; They'd have to be created there.constant-foldingisn't applied afterminify-type-constructorsdead-code-eliminationisn't applied afterminify-type-constructorsMath.pow-> Exponentiation Operator in ES2016 browsersDead code elimination: pure functions
Remove
windowwhere possible (unsafe if output is pasted inside another function)Merge multiple assignments
Remove boolean casting in conditions
Math.minandMath.maxwith two parametershttp://codegolf.stackexchange.com/a/60293/21413
Unsafe with
-0Various possible
indexOfoptimizationsOnly when accessing properties, compare to
undefinedinstead of usingtypeofRemoved because accessing a.bar could have side effects
Generate the string
'undefined'(I thinkconstant-foldingconflicts with this by transforming''+void 0back into'undefined')undefined-to-void->shorten-undefinedhttp://codegolf.stackexchange.com/a/42155/21413kangax commentedon Jul 6, 2016
Wow, closure is really nailing it, huh...
fregante commentedon Jul 6, 2016
I wanted to expand on that first "Resolve identity/simple functions" for CC since it does it quite nicely:
(click here to expand <details>)
And look at this crazy thing:
Which at times fails too:
This is being solved
But a slight change in the length of the text breaks it, even though the output would still be smaller, especially factoring gzip in
This was a wrong
repeatimplementation, but look at that outputNotes:
foois resolved. The loop is truncated, but it's still there.Making two calls no longer resolves
foo... foo = repeat('hello', 1) foo = repeat('hello', 1) ... closure output: (function(){function a(a,b){for(var c=0;c<b;c++);return b}foo=a("hello",1);foo=a("hello",1)})();jamiebuilds commentedon Jul 6, 2016
re: Use arrow functions (in ES6 browsers)
How should babel-minify handle minifying using modern language features that are normally compiled down to ES5 by Babel?
sebmck commentedon Jul 6, 2016
We can't turn function functions into arrow functions. It's an unsafe optimisation. Arrow functions have no
prototypeand cannot be used as constructors so they aren't the same.amasad commentedon Jul 6, 2016
James: I don't think that's necessary because you can always add the
plugins you need before the minify step
On Wed, Jul 6, 2016, 2:26 PM Sebastian McKenzie [email protected]
wrote:
jamiebuilds commentedon Jul 6, 2016
Yes, but there are places where ES5 code could be optimized more by using ES6+ features. I'm sure there are cases where we'd find arrow functions acceptable to use. Should optimizations like that be inferred? configured?
fregante commentedon Jul 7, 2016
Ideally the user would either be able to enable/disable each transform or use
browserlistto figure out which ES6 transforms are safe. But maybe atarget:"es6"could work.@kittens then maybe the function-to-arrow transform could only apply to functions that are verified to never user the prototype.
fregante commentedon Jul 7, 2016
It may be early for this, but should
babel-minifyhave a repo dedicated to transforms/plugin suggestions? One-suggestion-per-issue would make it easier to discuss, track and "rate" each transform (difficulty vs. how common it is vs. savings)Like: https://github.com/postcss/postcss-plugin-suggestion-box and https://github.com/sindresorhus/module-requests
Perhaps babel itself could benefit from one
boopathi commentedon Jul 19, 2016
A few more optimizations for conditionals -
propagate condition to the diff between consequent and alternate
conditional operator
kangax commentedon Jul 19, 2016
For the 1st case, I guess we would need to "fold" all the same-named methods as long as they're the only ones present in blocks and are in the same order?
but these won't work:
napa3um commentedon Sep 3, 2016
May be
undefined->''.$insteadundefined->void 0? ([].$,({}).$,eval.$,Date.$,Map.$, etc., or simplevar u;as global symbol.)fregante commentedon Sep 3, 2016
@napa3um Already suggested near the end of #25 (comment)
@hzoo Would it make sense to open a repo for the suggestions as suggested earlier? Or maybe just one-enhancement-per-issue so they can be easily discussed and tracked.
jokeyrhyme commentedon Sep 19, 2016
Came across this one, too: https://github.com/nolanlawson/optimize-js
Is there already a babel plugin for this?
kangax commentedon Oct 19, 2016
We should also turn
new ArraytoArray()no matter which arguments since they're identical and there's no need fornew/cc @shinewfregante commentedon Oct 19, 2016
It should happen already: source + repl
kangax commentedon Oct 19, 2016
So strange, I could swear it wasn't happening for me in repl.
kangax commentedon Oct 23, 2016
1000→1e3and so onpitaj commentedon Nov 22, 2016
I'd like to see unnecessary, single-use variables get inlined and also functions like this:
fregante commentedon Mar 23, 2017
That'd be great! Anything that isn't used more than once should be inlined. I often declare variables for readability but I could just as well write one-liners instead.
j-f1 commentedon Aug 8, 2017
These are shorter:
For property access:
Also, a couple other thoughts:
_classCallCheckhelper variable that makes surenewis used when constructing ES6 classes, Babili could create a_babiliUndefinedvariable that could get mangled down to a single character, which is significantly shorter thanvoid 0. By taking advantage of the same mechanism that Babel uses to generate a non-conflicting variable name, we could avoid problems when people name a variable_babiliUndefined. Example:Some of the things presented in https://codegolf.stackexchange.com/q/2682/44689:
Replace
parseInt/parseFloatwith+:Math.{floor,ceil,round}:boolean contexts:
!=→^:boolean contexts:
<thing>.length→0 in <thing>j-f1 commentedon Aug 22, 2017
Coming from #591, where @loganfsmyth suggested (after some discussion) that babel-minify place a
_void0or similar variable at the top, then replace all uses ofundefinedwith it, allowing for mangling:It should be possible to either put the
var _void0variable at the top ofsourceType: modulescripts or ones that are node modules. In other cases, they can be put at the top of function scopes, or the entire code could be wrapped in a block:This would increase code size if
void 0only appeared once, but would save 5 characters if used twice and 11 if used thrice.shanimal commentedon May 2, 2018
Replace
() => {}with globalnoop?