base-x icon indicating copy to clipboard operation
base-x copied to clipboard

Performance improvement

Open oscxc opened this issue 4 years ago • 2 comments

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

oscxc avatar May 07 '21 08:05 oscxc

can you show any benchmarks?

junderw avatar May 07 '21 11:05 junderw

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

`

oscxc avatar May 07 '21 14:05 oscxc

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 avatar Jan 27 '23 04:01 steveluscher

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

> [email protected] build
> tsc -p ./tsconfig.json ; standard --fix

> [email protected] 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

> [email protected] build
> tsc -p ./tsconfig.json ; standard --fix

> [email protected] 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 avatar Jan 27 '23 05:01 steveluscher

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

steveluscher avatar Feb 27 '23 04:02 steveluscher

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

junderw avatar Feb 27 '23 15:02 junderw