Skip to content

Commit 261fb36

Browse files
steveluscheroscxc
andcommitted
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 <[email protected]>
1 parent 39f2673 commit 261fb36

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

src/cjs/index.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function base (ALPHABET) {
8181
const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
8282
const b256 = new Uint8Array(size)
8383
// Process the characters.
84-
while (source[psz]) {
84+
while (psz < source.length) {
8585
// Decode character
8686
let carry = BASE_MAP[source.charCodeAt(psz)]
8787
// Invalid character

src/esm/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function base (ALPHABET) {
7979
const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
8080
const b256 = new Uint8Array(size)
8181
// Process the characters.
82-
while (source[psz]) {
82+
while (psz < source.length) {
8383
// Decode character
8484
let carry = BASE_MAP[source.charCodeAt(psz)]
8585
// Invalid character

ts_src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function base (ALPHABET: string): base.BaseConverter {
100100
const b256 = new Uint8Array(size)
101101

102102
// Process the characters.
103-
while (source[psz]) {
103+
while (psz < source.length) {
104104
// Decode character
105105
let carry = BASE_MAP[source.charCodeAt(psz)]
106106

0 commit comments

Comments
 (0)