oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.16k stars 2.77k forks source link

Poor performance for crypto's privateEncrypt/publicDecrypt #6076

Closed naijun0403 closed 1 month ago

naijun0403 commented 1 year ago

What version of Bun is running?

1.0.3+25e69c71e70ac8a0a88f9cf15b4057bd7b2a633a

What platform is your computer?

Darwin 22.6.0 x86_64 i386

What steps can reproduce the bug?

You just need to run this code with node and bun.

const crypto = require('crypto');
const fs = require('fs');

const data = 'Hello, world!';
const iterations = 1000;

const privateKeyPath = './private_key.pem';
const publicKeyPath = './public_key.pem';

function benchmarkCrypto() {
  console.log(`Benchmarking crypto performance using key files...`);
  const start = Date.now();

  for (let i = 0; i < iterations; i++) {
    const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
    const publicKey = fs.readFileSync(publicKeyPath, 'utf8');

    const encrypted = crypto.privateEncrypt(privateKey, Buffer.from(data));

    const decrypted = crypto.publicDecrypt(publicKey, encrypted);
  }

  const end = Date.now();
  const duration = end - start;
  console.log(`Time taken: ${duration} ms`);
}

benchmarkCrypto();

What is the expected behavior?

At least the speed should be similar to node.

What do you see instead?

Bun is much slower than node in the RSA encryption and decryption part.

Benchmark 1: node test
  Time (mean ± σ):      1.013 s ±  0.015 s    [User: 0.964 s, System: 0.061 s]
  Range (min … max):    0.986 s …  1.036 s    10 runs

Benchmark 2: bun test.js
  Time (mean ± σ):     14.536 s ±  0.247 s    [User: 14.638 s, System: 0.296 s]
  Range (min … max):   14.147 s … 15.020 s    10 runs

Summary
  node test ran
   14.35 ± 0.32 times faster than bun test.js

Additional information

Obviously, I also evaluated the file I/O process when benchmarking. however. This does not affect performance. Bun is slightly faster than node.

I used RSA 2048.

Jarred-Sumner commented 1 year ago

These are currently from the browserify polyfills which means they aren't implemented in native code and are much slower as a result. We need to make them use BoringSSL instead (as we have done for other parts of node:crypto)

naijun0403 commented 1 year ago

Okay, I'll always be waiting for news

gornostay25 commented 10 months ago

@naijun0403 @Jarred-Sumner Any updates?

eL1x00r commented 10 months ago

up

tecoad commented 1 month ago

Up