rollup-plugin-optimize-arguments
v0.2.1
Published
Rollup plugin to avoid leaking `arguments` object
Downloads
11
Maintainers
Readme
rollup-plugin-optimize-arguments
Rollup plugin to analyze for usage of arguments
and automatically convert it to an array, to avoid leaking the arguments
object (which is an optimization killer).
Basically, you can just use arguments
as if it were a normal array and this plugin will take care of the rest.
Installation
npm install --save-dev rollup-plugin-optimize-arguments
Usage
var rollup = require('rollup').rollup;
var optimizeArguments = require('rollup-plugin-optimize-arguments');
rollup({
entry: 'main.js',
plugins: [
optimizeArguments()
]
}).then(...)
Examples
Example input:
function foobar() {
return arguments;
}
Output:
function foobar() {
var $_len = arguments.length, $_args = new Array($_len); while ($_len--) { $_args[$_len] = arguments[$_len]; }
return $_args;
}
Or even nested:
function foobar() {
console.log(arguments);
(function foobaz() {
console.log(arguments);
(() => {
console.log(arguments);
var toto = function () {
console.log(arguments);
!function() {
console.log(arguments);
}()
}
toto();
})();
})();
}
Output:
function foobar() {
var $_len = arguments.length, $_args = new Array($_len); while ($_len--) { $_args[$_len] = arguments[$_len]; }
console.log($_args);
(function foobaz() {
var $_len = arguments.length, $_args = new Array($_len); while ($_len--) { $_args[$_len] = arguments[$_len]; }
console.log($_args);
(() => {
console.log($_args);
var toto = function () {
var $_len = arguments.length, $_args = new Array($_len); while ($_len--) { $_args[$_len] = arguments[$_len]; }
console.log($_args);
!function() {
var $_len = arguments.length, $_args = new Array($_len); while ($_len--) { $_args[$_len] = arguments[$_len]; }
console.log($_args);
}();
};
toto();
})();
})();
}
(Arrow functions do not create their own scope and thus inherit their parent's arguments
.)
This plugin also intelligently avoids creating unnecessary $_args
objects in cases where usage is safe:
function foobar() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
Credits
Thanks to Rich Harris for rollup-plugin-inject and Alex Lur for rollup-plugin-ignore which I based this codebase off of.