Skip to content

Commit

Permalink
Improve decoding performance
Browse files Browse the repository at this point in the history
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 cryptocoinjs#74.
  • Loading branch information
steveluscher committed Apr 13, 2023
1 parent dd4c308 commit e24608f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ts_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down

0 comments on commit e24608f

Please sign in to comment.