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:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user