Skip to content

Commit

Permalink
Merge pull request #55 from UmamiAppearance/speed-improvements
Browse files Browse the repository at this point in the history
Speed improvements
  • Loading branch information
UmamiAppearance authored May 26, 2023
2 parents 2f03a21 + 5b802a8 commit 2ad46c3
Show file tree
Hide file tree
Showing 80 changed files with 548 additions and 441 deletions.
51 changes: 29 additions & 22 deletions cjs/base-ex.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ class BaseConverter {
}

this.decPadVal = decPadVal;
this.powers = {};
}

/**
Expand Down Expand Up @@ -1151,10 +1152,9 @@ class BaseConverter {
return new Uint8Array(0);
}


let bs = this.bsDec;
const byteArray = new Array();

const byteArray = [];
[...inputBaseStr].forEach(c => {
const index = charset.indexOf(c);
if (index > -1) {
Expand All @@ -1163,6 +1163,7 @@ class BaseConverter {
throw new DecodingError(c);
}
});


let padChars;

Expand All @@ -1187,17 +1188,23 @@ class BaseConverter {
// the blocksize.

for (let i=0, l=byteArray.length; i<l; i+=bs) {

// Build a subarray of bs bytes.
let n = 0n;

for (let j=0; j<bs; j++) {
n += BigInt(byteArray[i+j]) * this.pow(bs-1-j);
const exp = bs-1-j;
const pow = this.powers[exp] || (() => {
this.powers[exp] = BigInt(this.pow(exp));
return this.powers[exp];
})();

n += BigInt(byteArray[i+j]) * pow;
}

// To store the output chunks, initialize a
// new default array.
const subArray256 = new Array();
const subArray256 = [];

// The subarray gets converted into a bs*8-bit
// binary number "n", most significant byte
Expand Down Expand Up @@ -1225,7 +1232,7 @@ class BaseConverter {

// The subarray gets concatenated with the
// main array.
b256Array = b256Array.concat(subArray256);
b256Array.push(...subArray256);
}

// Remove padded zeros (or in case of LE all leading zeros)
Expand Down Expand Up @@ -1453,7 +1460,7 @@ class BaseTemplate {
/**
* [BaseEx|Base1 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-1.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1610,7 +1617,7 @@ class Base1 extends BaseTemplate {
/**
* [BaseEx|Base16 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-16.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1699,7 +1706,7 @@ class Base16 extends BaseTemplate {
/**
* [BaseEx|Base32 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-32.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1805,7 +1812,7 @@ class Base32 extends BaseTemplate {
/**
* [BaseEx|Base58 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-58.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1956,7 +1963,7 @@ class Base58 extends BaseTemplate{
/**
* [BaseEx|Base64 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-64.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2045,7 +2052,7 @@ class Base64 extends BaseTemplate {
/**
* [BaseEx|UUencode Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/uuencode.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2258,7 +2265,7 @@ const ees = () => {
/**
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2382,7 +2389,7 @@ class Base85 extends BaseTemplate {
/**
* [BaseEx|Base91 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-91.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT AND BSD-3-Clause (Base91, Copyright (c) 2000-2006 Joachim Henke)
*/
Expand Down Expand Up @@ -2616,7 +2623,7 @@ class Base91 extends BaseTemplate {
/**
* [BaseEx|LEB128 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/leb-128.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2782,7 +2789,7 @@ class LEB128 extends BaseTemplate {
/**
* [BaseEx|Ecoji Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/ecoji.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT OR Apache-2.0
* @see https://github.com/keith-turner/ecoji
Expand Down Expand Up @@ -3125,7 +3132,7 @@ class Ecoji extends BaseTemplate {
/**
* [BaseEx|Base2048 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-2048.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3304,7 +3311,7 @@ class Base2048 extends BaseTemplate {
/**
* [BaseEx|SimpleBase Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/simple-base.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3405,7 +3412,7 @@ let DP=20,RM=1,MAX_DP=1e6,NE=-7,PE=21,STRICT=!1,NAME="[big.js] ",INVALID=NAME+"I
/**
* [BaseEx|BasePhi Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-phi.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3743,7 +3750,7 @@ class BasePhi extends BaseTemplate {
/**
* [BaseEx|Byte Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/byte-converter.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3862,7 +3869,7 @@ class ByteConverter {
/**
* [BaseEx]{@link https://github.com/UmamiAppearance/BaseExJS}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down
2 changes: 1 addition & 1 deletion cjs/base-ex.cjs.map

Large diffs are not rendered by default.

51 changes: 29 additions & 22 deletions dist/base-ex.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ class BaseConverter {
}

this.decPadVal = decPadVal;
this.powers = {};
}

/**
Expand Down Expand Up @@ -1149,10 +1150,9 @@ class BaseConverter {
return new Uint8Array(0);
}


let bs = this.bsDec;
const byteArray = new Array();

const byteArray = [];
[...inputBaseStr].forEach(c => {
const index = charset.indexOf(c);
if (index > -1) {
Expand All @@ -1161,6 +1161,7 @@ class BaseConverter {
throw new DecodingError(c);
}
});


let padChars;

Expand All @@ -1185,17 +1186,23 @@ class BaseConverter {
// the blocksize.

for (let i=0, l=byteArray.length; i<l; i+=bs) {

// Build a subarray of bs bytes.
let n = 0n;

for (let j=0; j<bs; j++) {
n += BigInt(byteArray[i+j]) * this.pow(bs-1-j);
const exp = bs-1-j;
const pow = this.powers[exp] || (() => {
this.powers[exp] = BigInt(this.pow(exp));
return this.powers[exp];
})();

n += BigInt(byteArray[i+j]) * pow;
}

// To store the output chunks, initialize a
// new default array.
const subArray256 = new Array();
const subArray256 = [];

// The subarray gets converted into a bs*8-bit
// binary number "n", most significant byte
Expand Down Expand Up @@ -1223,7 +1230,7 @@ class BaseConverter {

// The subarray gets concatenated with the
// main array.
b256Array = b256Array.concat(subArray256);
b256Array.push(...subArray256);
}

// Remove padded zeros (or in case of LE all leading zeros)
Expand Down Expand Up @@ -1451,7 +1458,7 @@ class BaseTemplate {
/**
* [BaseEx|Base1 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-1.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1608,7 +1615,7 @@ class Base1 extends BaseTemplate {
/**
* [BaseEx|Base16 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-16.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1697,7 +1704,7 @@ class Base16 extends BaseTemplate {
/**
* [BaseEx|Base32 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-32.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1803,7 +1810,7 @@ class Base32 extends BaseTemplate {
/**
* [BaseEx|Base58 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-58.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -1954,7 +1961,7 @@ class Base58 extends BaseTemplate{
/**
* [BaseEx|Base64 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-64.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2043,7 +2050,7 @@ class Base64 extends BaseTemplate {
/**
* [BaseEx|UUencode Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/uuencode.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2256,7 +2263,7 @@ const ees = () => {
/**
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2380,7 +2387,7 @@ class Base85 extends BaseTemplate {
/**
* [BaseEx|Base91 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-91.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT AND BSD-3-Clause (Base91, Copyright (c) 2000-2006 Joachim Henke)
*/
Expand Down Expand Up @@ -2614,7 +2621,7 @@ class Base91 extends BaseTemplate {
/**
* [BaseEx|LEB128 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/leb-128.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -2780,7 +2787,7 @@ class LEB128 extends BaseTemplate {
/**
* [BaseEx|Ecoji Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/ecoji.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT OR Apache-2.0
* @see https://github.com/keith-turner/ecoji
Expand Down Expand Up @@ -3123,7 +3130,7 @@ class Ecoji extends BaseTemplate {
/**
* [BaseEx|Base2048 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-2048.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3302,7 +3309,7 @@ class Base2048 extends BaseTemplate {
/**
* [BaseEx|SimpleBase Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/simple-base.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3403,7 +3410,7 @@ let DP=20,RM=1,MAX_DP=1e6,NE=-7,PE=21,STRICT=!1,NAME="[big.js] ",INVALID=NAME+"I
/**
* [BaseEx|BasePhi Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-phi.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3741,7 +3748,7 @@ class BasePhi extends BaseTemplate {
/**
* [BaseEx|Byte Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/byte-converter.js}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down Expand Up @@ -3860,7 +3867,7 @@ class ByteConverter {
/**
* [BaseEx]{@link https://github.com/UmamiAppearance/BaseExJS}
*
* @version 0.7.8
* @version 0.7.9
* @author UmamiAppearance [[email protected]]
* @license MIT
*/
Expand Down
Loading

0 comments on commit 2ad46c3

Please sign in to comment.