From 261fb3636147815a7705375cfe7c1217db15e8f5 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Thu, 13 Apr 2023 05:04:21 +0000 Subject: [PATCH] Improve decoding performance This yields a performance improvement over indexing into `source` over and over. `source` is asserted to be a string, `psz` is monotonically increasing, and `psz >= source.length` is a good indication that you've run out of characters. Originally proposed by @oscxc in #74. Co-authored-by: Rand <15077550@qq.com> --- src/cjs/index.cjs | 2 +- src/esm/index.js | 2 +- ts_src/index.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cjs/index.cjs b/src/cjs/index.cjs index 0124347..07524c6 100644 --- a/src/cjs/index.cjs +++ b/src/cjs/index.cjs @@ -81,7 +81,7 @@ function base (ALPHABET) { const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up. const b256 = new Uint8Array(size) // Process the characters. - while (source[psz]) { + while (psz < source.length) { // Decode character let carry = BASE_MAP[source.charCodeAt(psz)] // Invalid character diff --git a/src/esm/index.js b/src/esm/index.js index ac60db8..ce48a14 100644 --- a/src/esm/index.js +++ b/src/esm/index.js @@ -79,7 +79,7 @@ function base (ALPHABET) { const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up. const b256 = new Uint8Array(size) // Process the characters. - while (source[psz]) { + while (psz < source.length) { // Decode character let carry = BASE_MAP[source.charCodeAt(psz)] // Invalid character diff --git a/ts_src/index.ts b/ts_src/index.ts index 66e0e4b..5f2431a 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -100,7 +100,7 @@ function base (ALPHABET: string): base.BaseConverter { const b256 = new Uint8Array(size) // Process the characters. - while (source[psz]) { + while (psz < source.length) { // Decode character let carry = BASE_MAP[source.charCodeAt(psz)]