babel-preset-more-optimization
v0.0.6
Published
Babel preset for additional optimization/minification.
Downloads
5
Readme
babel-preset-more-optimization
Babel preset for additional optimization/minification.
If you stumble across this: NOT FOR PRODUCTION USE
Installation
npm install --save-dev babel-preset-more-optimization
Usage
.babelrc:
{
"presets": [
"more-optimization"
]
}
Or, with the unsafe
option:
{
"presets": [
["more-optimization", { "unsafe": true }]
]
}
...its behaviour is described in the following sections.
Optimizations
Flatten trivial IIFEs
In:
(() => {
foo();
bar.baz();
})();
var x = (function() {
return 'x';
})();
(function() {
var someBinding;
if (or_someNontrivialControlFlow) bailout();
})();
Out:
foo();
bar.baz();
var x = 'x';
(function() {
var someBinding;
if (or_someNontrivialControlFlow) bailout();
})();
Eliminate calls of identity functions
In:
function id(x) { return x; }
var foo = id(baz(), 1, '2');
var bar = id(baz(), someOtherImpureArgument());
Out:
function id(x) { return x; }
var foo = baz();
var bar = id(baz(), someOtherImpureArgument());
unsafe
option
With unsafe
, calls of identity functions whose arguments are impure due to unreferenced identifiers are eliminated.
This is unlikely to be unsafe in practice, as you'd have to be relying on side-effects of a global getter or a thrown ReferenceError
.
In:
function toClass(val, class_) { return val; }
var x = toClass(a, HTMLAnchorElement);
Out:
function toClass(val, class_) { return val; }
var x = a;
Eliminate calls of Object.freeze
and friends
This is intended primarily to facilitate SROA (below).
In:
var x = Object.freeze({ a: 5 });
Out:
var x = { a: 5 };
Scalar replacement of aggregates
In:
var x = {
a: { b: 5 },
c() {}
};
Out:
var _x$a$b = 5,
_x$c = function c() {};
unsafe
option
With unsafe
, scalar replacement will be performed on objects whose properties are written to.
This is unlikely to be unsafe in practice, as the assigned property (alone) would have to reference this
.
(This optimization always bails out if existing object properties reference this
.)
In:
var x = {
a: { b: 5 },
c() {}
};
x.a.b *= 2;
Out:
var _x$a$b = 5,
_x$c = function c() {};
_x$a$b *= 2;
Store-to-load forwarding / copy propagation
In:
var a = () => {};
var _a = a;
function foo() {
var b = _a;
function bar() {
var c = b;
c();
}
}
Out:
var a = () => {};
function foo() {
function bar() {
a();
}
}