Skip to content
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

perf(mangler): improve mangleScope perf #854

Merged
merged 1 commit into from May 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Expand Up @@ -320,18 +320,13 @@ module.exports = babel => {
}
mangler.visitedScopes.add(scope);

// Helpers to generate names
let i = 0;
function getNext() {
return mangler.charset.getIdentifier(i++);
}
function resetNext() {
i = 0;
}

const bindings = scopeTracker.bindings.get(scope);
const names = [...bindings.keys()];

// A counter to generate names and reset
// so we can reuse removed names
let counter = 0;

/**
* 1. Iterate through the list of BindingIdentifiers
* 2. Rename each of them in-place
Expand All @@ -341,18 +336,27 @@ module.exports = babel => {
* because (2) we rename in place and update the bindings
* as we traverse through the keys
*/
for (let i = 0; i < names.length; i++) {
const oldName = names[i];
for (const oldName of names) {
const binding = bindings.get(oldName);

if (mangler.canMangle(oldName, binding, scope)) {
let next;
do {
next = getNext();
next = mangler.charset.getIdentifier(counter++);
} while (!mangler.isValidName(next, binding, scope));

// Reset so variables which are removed can be reused
resetNext();
//
// the following is an assumtion (for perf)
// the length 3 is an assumption that if the oldName isn't
// 1 or 2 characters, then probably we are not going to find
// a name - because for almost all usecases we have 1 or 2
// character new names only. And for the edge cases where
// one scope has lots and lots of variables, it's okay to
// name something with 3 characters instead of 1
if (oldName.length < 3) {
counter = 0;
}

// Once we detected a valid `next` Identifier which could be used,
// call the renamer
Expand Down