Skip to content

Commit 0a35446

Browse files
steveluscheroscxc
authored 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 4c10d33 commit 0a35446

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/index.js

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

ts_src/index.ts

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

9999
// Process the characters.
100-
while (source[psz]) {
100+
while (psz < source.length) {
101101
// Decode character
102102
let carry = BASE_MAP[source.charCodeAt(psz)]
103103

0 commit comments

Comments
 (0)