-
Notifications
You must be signed in to change notification settings - Fork 187
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
9x slower than jsbn #181
Comments
I haven't looked at it carefully yet, but my guess is that the performance difference comes from parsing hex input. From what I understand, jsbn internally uses base representation that is a power of 2, while this library internally uses a base representation that is a power of 10. This means that jsbn will have a significant advantage in parsing hexadecimal input, and this library will have a significant advantage in parsing decimal input. Unfortunately it's a tradeoff, and I can't think of a simple way to make both fast at the same time. I decided to optimize for decimal since I figured it would be a more common use case (I can't say for sure if that's true or not), but that means this library can't really compete with other libraries that optimize for powers of 2. |
Not really, in my tests i has nothing with hex parsing. modPow operation is simply super slow. |
modPow very slow indeed! it takes 20 seconds to complete (for browsers that don't support native BigInt) |
I'm using an npm library which is based on jsbn. This pairing works together quite well, but it's been difficult to work with due to the
jsbn
library's thin documentation, especially around its ctor, which has a screwy parameter interface.I therefore went searching for replacements and found your library. I was excited to see that it's got docs, its ctor makes more sense than that of
jsbn
, it's getting updates, it implements a nearly drop-in compatible interface, and on top of all of that it acts as a polyfill for a feature that will appear in JS platforms that are still in my future. (Alas!) This thing is dripping awesomesauce!Then I benchmarked it. On my tests here, it's about 9x slower than
jsbn
.I'll accept some slowdown to get the above features, but not that much. With
jsbn
, a key element of the calculation takes about a tenth of a second, which means it adds a barely noticeable delay to the user. Multiplying that delay by 10 makes the delay actually annoying.I'm filing this issue along with the tests below in case there's a way you can substantially speed up this library short of delegating to native
BigInt
, perhaps by swiping ideas fromjsbn
. NativeBigInt
will be great, but I probably won't be able to depend on it until about a year or so from now, when it's in a sufficiently large fraction of the browsers my customers use.Thanks!
Simple Node test case:
Install the required modules with:
Run it as-is above, then apply this trivial diff to convert the
secure-remote-password
module to use yourbig-integer
module and run it again:On CentOS 7, which includes Node 6.16.0, I get 10.92 iterations per second with the
jsbn
based implementation and 1.22 ops/sec withbigInt
.On a macOS system running Node 11.14.0 from Homebrew, I get 18.21 ops/sec with the
jsbn
implementation and 58.67 withbigInt
, which sounds great until you realize that it's because Node 11.14 includes native big integer support. If I hack your library code to setsupportsNativeBigInt
tofalse
, the benchmark result drops to 2.15 ops/sec here, only slightly better than on the CentOS 7 system, percentage-wise.I'm tempted to rely on native BigInt support, but I worry that I've got a lot of users that won't have it yet, and they'll have a bad user experience.
The text was updated successfully, but these errors were encountered: