From e24608ffe14acd62508dc894ff4e77b19d825575 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. --- src/index.js | 2 +- ts_src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 11e1a13..3f4bcab 100644 --- a/src/index.js +++ b/src/index.js @@ -80,7 +80,7 @@ function base (ALPHABET) { var size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up. var b256 = new Uint8Array(size) // Process the characters. - while (source[psz]) { + while (psz < source.length) { // Decode character var carry = BASE_MAP[source.charCodeAt(psz)] // Invalid character diff --git a/ts_src/index.ts b/ts_src/index.ts index 2e71501..8c91160 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -99,7 +99,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)]