Browse Source

Increased `identifierNamesGenerator: 'mangled` speed

sanex3339 5 years ago
parent
commit
58c165f889

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ v0.24.0
 * **Internal refactoring:** completely new mechanism to rename variable names
 * Dynamic import and `import.meta` support. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/505
 * Now usage of some browser-related options with `target: 'node'` will cause a validation error
+* Increased `identifierNamesGenerator: 'mangled` speed
 * **CLI:** a file path will be displayed on obfuscation error. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/513
 * Fixed many `transformObjectKeys` runtime errors
 * Fixed `Maximum call stack size exceeded` error on large strings when `splitString` option is enabled

File diff suppressed because it is too large
+ 0 - 0
dist/index.browser.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.cli.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 4 - 4
src/generators/identifier-names-generators/AbstractIdentifierNamesGenerator.ts

@@ -18,9 +18,9 @@ export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNam
     protected readonly randomGenerator: IRandomGenerator;
 
     /**
-     * @type {Array}
+     * @type {Set<string>}
      */
-    protected readonly preservedNames: string[] = [];
+    protected readonly preservedNamesSet: Set<string> = new Set();
 
     /**
      * @param {IRandomGenerator} randomGenerator
@@ -51,7 +51,7 @@ export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNam
      * @returns {void}
      */
     public preserveName (name: string): void {
-        this.preservedNames.push(name);
+        this.preservedNamesSet.add(name);
     }
 
     /**
@@ -59,7 +59,7 @@ export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNam
      * @returns {boolean}
      */
     public isValidIdentifierName (name: string): boolean {
-        return this.notReservedName(name) && !this.preservedNames.includes(name);
+        return this.notReservedName(name) && !this.preservedNamesSet.has(name);
     }
 
     /**

+ 7 - 6
src/generators/identifier-names-generators/MangledIdentifierNamesGenerator.ts

@@ -21,13 +21,13 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
     /**
      * Reserved JS words with length of 2-4 symbols that can be possible generated with this replacer
      *
-     * @type {string[]}
+     * @type {Set<string>}
      */
-    private static readonly reservedNames: string[] = [
+    private static readonly reservedNamesSet: Set<string> = new Set([
         'byte', 'case', 'char', 'do', 'else', 'enum', 'eval', 'for', 'goto',
         'if', 'in', 'int', 'let', 'long', 'new', 'null', 'this', 'true', 'try',
         'var', 'void', 'with'
-    ];
+    ]);
 
     /**
      * @type {string}
@@ -77,7 +77,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
      */
     public isValidIdentifierName (mangledName: string): boolean {
         return super.isValidIdentifierName(mangledName)
-            && !MangledIdentifierNamesGenerator.reservedNames.includes(mangledName);
+            && !MangledIdentifierNamesGenerator.reservedNamesSet.has(mangledName);
     }
 
     /**
@@ -87,6 +87,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
     private generateNewMangledName (previousMangledName: string): string {
         const generateNewMangledName: (name: string) => string = (name: string): string => {
             const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
+            const nameSequenceLength: number = nameSequence.length;
             const nameLength: number = name.length;
 
             const zeroSequence: (num: number) => string = (num: number): string => {
@@ -96,9 +97,9 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
             let index: number = nameLength - 1;
 
             do {
-                const character: string = name.charAt(index);
+                const character: string = name[index];
                 const indexInSequence: number = nameSequence.indexOf(character);
-                const lastNameSequenceIndex: number = nameSequence.length - 1;
+                const lastNameSequenceIndex: number = nameSequenceLength - 1;
 
                 if (indexInSequence !== lastNameSequenceIndex) {
                     const previousNamePart: string = name.substring(0, index);

Some files were not shown because too many files changed in this diff