Skip to content

Commit

Permalink
Mangler performance - some improvement (#134)
Browse files Browse the repository at this point in the history
* Mangler perf

* Optimize collect visitor and avoid traversing bindings twice
  • Loading branch information
boopathi authored and kangax committed Aug 31, 2016
1 parent ba4e65e commit de07c86
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = ({ types: t }) => {
collect() {
const mangler = this;

this.program.traverse({
const collectVisitor = {
// capture direct evals
CallExpression(path) {
const callee = path.get("callee");
Expand All @@ -50,24 +50,28 @@ module.exports = ({ types: t }) => {
) {
mangler.markUnsafeScopes(path.scope);
}
},
}
};

if (this.charset.shouldConsider) {
// charset considerations
Identifier(path) {
collectVisitor.Identifier = function Identifier(path) {
const { node } = path;

if ((path.parentPath.isMemberExpression({ property: node })) ||
(path.parentPath.isObjectProperty({ key: node }))
) {
mangler.charset.consider(node.name);
}
},
};

// charset considerations
Literal({ node }) {
collectVisitor.Literal = function Literal({ node }) {
mangler.charset.consider(String(node.value));
}
});
};
}

this.program.traverse(collectVisitor);
}

mangle() {
Expand Down Expand Up @@ -103,26 +107,37 @@ module.exports = ({ types: t }) => {
// i = 0;
// }

Object
.keys(scope.getAllBindings())
.filter((b) => {
const binding = scope.getBinding(b);

return scope.hasOwnBinding(b)
&& !binding.path.isLabeledStatement()
&& !mangler.isBlacklist(b, mangler.blacklist)
&& (mangler.keepFnames ? !isFunction(binding.path) : true);
})
.map((b) => {
let next;
do {
next = getNext();
} while (!t.isValidIdentifier(next) || scope.hasBinding(next) || scope.hasGlobal(next) || scope.hasReference(next));
// TODO:
// re-enable this
// resetNext();
mangler.rename(scope, b, next);
});
const bindings = scope.getAllBindings();
const names = Object.keys(bindings);

for (let i = 0; i < names.length; i++) {
const oldName = names[i];
const binding = bindings[oldName];

if (
!scope.hasOwnBinding(oldName)
|| binding.path.isLabeledStatement()
|| mangler.isBlacklist(oldName)
|| (mangler.keepFnames ? isFunction(binding.path) : false)
) {
continue;
}

let next;
do {
next = getNext();
} while (
!t.isValidIdentifier(next)
|| hop.call(bindings, next)
|| scope.hasGlobal(next)
|| scope.hasReference(next)
);

// TODO:
// re-enable this - check above
// resetNext();
mangler.rename(scope, oldName, next);
}
}
});

Expand Down

0 comments on commit de07c86

Please # to comment.