mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added optimisations to numbersToExpressions option. Updated tests
This commit is contained in:
Vendored
+2
-2
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+12
-2
@@ -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}
|
||||
*/
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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);
|
||||
`,
|
||||
|
||||
@@ -810,6 +810,7 @@ describe('JavaScriptObfuscator', () => {
|
||||
deadCodeInjection: true,
|
||||
deadCodeInjectionThreshold: 1,
|
||||
disableConsoleOutput: false,
|
||||
numbersToExpressions: true,
|
||||
simplify: true,
|
||||
renameProperties: true,
|
||||
rotateStringArray: true,
|
||||
|
||||
@@ -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'],
|
||||
|
||||
Reference in New Issue
Block a user