Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement #74

Closed
wants to merge 1 commit into from
Closed

Performance improvement #74

wants to merge 1 commit into from

Conversation

oscxc
Copy link
Contributor

@oscxc oscxc commented May 7, 2021

Just change (source[psz]) to (psz < source.length) in line 82.

Just change (source[psz]) to (psz < source.length) in line 82.
@junderw
Copy link
Member

junderw commented May 7, 2021

can you show any benchmarks?

@oscxc
Copy link
Contributor Author

oscxc commented May 7, 2021

can you show any benchmarks?

`const rand = (a, b) => {
return Math.floor(Math.random() * (b - a + 1) + a);
};
let BaseX = require('./basex')('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');

let buffer = Buffer.alloc(16);
for(let i = 0; i < 16; i++){
buffer[i] = rand(0, 255);
}
let str = BaseX.encode(buffer);
let buffer2 = BaseX.decode(str);
console.log(buffer.equals(buffer2)); //true

let t1 = new Date().getTime();
for (let i = 0; i < 1000000; i++){
let test = BaseX.decode(str);
}
let t2 = new Date().getTime();
console.log(t2 - t1);

//cpu i9-9900kf

//use present (source[psz])
//593ms, 554ms, 596ms, 607ms, 605ms, 597ms, 627ms, 627ms, 596ms, 622ms

//change to (psz < source.length)
//546ms, 539ms, 563ms, 541ms, 566ms, 546ms, 569ms, 543ms, 539ms, 537ms

`

@steveluscher
Copy link
Contributor

This makes a ton of sense to me. 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.

Any objection to merging this?

@steveluscher
Copy link
Contributor

The effect on decode seems quite significant. Using this repo's benchmark suite:

Before:

~/src/base-x/benchmark$ (cd ../ && npm run build) && SEED=8854dc2a353e143702ef1b29874b63a4 npm start

> base-x@4.0.0 build
> tsc -p ./tsconfig.json ; standard --fix

> base-x-benchmark@0.0.0 start
> node index.js

Seed: 8854dc2a353e143702ef1b29874b63a4
--------------------------------------------------
encode x 389,085 ops/sec ±0.34% (9 runs sampled)
decode x 427,013 ops/sec ±0.31% (8 runs sampled)
==================================================

After:

~/src/base-x/benchmark$ (cd ../ && npm run build) && SEED=8854dc2a353e143702ef1b29874b63a4 npm start

> base-x@4.0.0 build
> tsc -p ./tsconfig.json ; standard --fix

> base-x-benchmark@0.0.0 start
> node index.js

Seed: 8854dc2a353e143702ef1b29874b63a4
--------------------------------------------------
encode x 389,681 ops/sec ±0.32% (8 runs sampled)
decode x 448,309 ops/sec ±0.09% (9 runs sampled)
==================================================

@steveluscher
Copy link
Contributor

Feel like slamming this in, @junderw? This should make tons of downstream deps faster!

@junderw
Copy link
Member

junderw commented Feb 27, 2023

Travis CI is broken, needs to be moved to Github CI.

@junderw junderw closed this Feb 27, 2023
steveluscher added a commit to steveluscher/base-x that referenced this pull request Apr 13, 2023
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.
steveluscher added a commit to steveluscher/base-x that referenced this pull request Apr 13, 2023
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.
steveluscher added a commit to steveluscher/base-x that referenced this pull request Apr 13, 2023
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.

Co-authored-by: Rand <15077550@qq.com>
steveluscher added a commit to steveluscher/base-x that referenced this pull request Jul 3, 2024
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.

Co-authored-by: Rand <15077550@qq.com>
junderw pushed a commit that referenced this pull request Jul 4, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants