Skip to content

Commit

Permalink
Added optimisations to numbersToExpressions option. Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Jul 12, 2020
1 parent dd8dcac commit 1beb3cd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class NumberNumericalExpressionAnalyzer implements INumberNumericalExpres
*/
private static readonly additionalParts: number = 3;

/**
* @type {Map<number, number[]>}
*/
private readonly numberFactorsMap: Map<number, number[]> = new Map();

/**
* @type {IRandomGenerator}
*/
Expand Down Expand Up @@ -103,11 +108,16 @@ export class NumberNumericalExpressionAnalyzer implements INumberNumericalExpres
private mixWithMultiplyParts (number: number): number | number[] {
const shouldMixWithMultiplyParts: boolean = this.randomGenerator.getMathRandom() > 0.5;

if (!shouldMixWithMultiplyParts) {
if (!shouldMixWithMultiplyParts || number === 0) {
return number;
}

const factors: number[] = NumberUtils.getFactors(number);
let factors: number[] | null = this.numberFactorsMap.get(number) ?? null;

if (!factors) {
factors = NumberUtils.getFactors(number);
this.numberFactorsMap.set(number, factors);
}

if (!factors.length) {
return number;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/NumberUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ export class NumberUtils {

const factors: number[] = [];

const root: number = Math.sqrt(number);
const isEven: boolean = number % 2 === 0;
const incrementValue: number = isEven ? 1 : 2;

for (
let currentFactor = 1;
Math.pow(currentFactor, 2) <= number;
currentFactor <= root;
currentFactor += incrementValue
) {
if (number % currentFactor !== 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
const foo = 100;
const foo = 0;
console.log(foo);
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ describe('JavaScriptObfuscator', () => {
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
disableConsoleOutput: false,
numbersToExpressions: true,
simplify: true,
renameProperties: true,
rotateStringArray: true,
Expand Down
1 change: 1 addition & 0 deletions test/runtime-tests/JavaScriptObfuscatorRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('JavaScriptObfuscator runtime eval', function () {
debugProtection: true,
disableConsoleOutput: true,
domainLock: ['obfuscator.io'],
numbersToExpressions: true,
simplify: true,
renameProperties: true,
reservedNames: ['generate', 'sha256'],
Expand Down

0 comments on commit 1beb3cd

Please sign in to comment.