-
-
Save panva/637497b3e612b996dc6661650bde37fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
secret transfer x 31,701 ops/sec ±4.30% (78 runs sampled) | |
secret export-import x 27,923 ops/sec ±5.50% (76 runs sampled) | |
private rsa transfer x 30,459 ops/sec ±8.98% (79 runs sampled) | |
private rsa der export-import x 16,252 ops/sec ±1.72% (79 runs sampled) | |
public rsa transfer x 31,965 ops/sec ±3.18% (84 runs sampled) | |
public rsa der export-import x 19,767 ops/sec ±1.74% (79 runs sampled) | |
private ec transfer x 31,345 ops/sec ±4.23% (81 runs sampled) | |
private ec der export-import x 9,483 ops/sec ±1.32% (81 runs sampled) | |
public ec transfer x 31,025 ops/sec ±6.02% (83 runs sampled) | |
public ec der export-import x 10,384 ops/sec ±1.08% (83 runs sampled) | |
private ed25519 transfer x 30,734 ops/sec ±5.83% (82 runs sampled) | |
private ed25519 der export-import x 10,699 ops/sec ±0.95% (81 runs sampled) | |
public ed25519 transfer x 31,406 ops/sec ±5.35% (80 runs sampled) | |
public ed25519 der export-import x 22,013 ops/sec ±1.36% (81 runs sampled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Worker, isMainThread, parentPort } = require('worker_threads') | |
const crypto = require('crypto') | |
if (isMainThread) { | |
const tasks = new Map() | |
let worker | |
let taskId = 0 | |
const spawn = () => { | |
worker = new Worker(__filename) | |
worker.on('message', function ({ id }) { | |
const task = tasks.get(id) | |
tasks.delete(id) | |
if (tasks.size === 0) worker.unref() | |
task() | |
}) | |
} | |
const send = (data) => new Promise((resolve) => { | |
const id = taskId++ | |
tasks.set(id, resolve) | |
if (worker === undefined) spawn() | |
worker.ref() | |
worker.postMessage({ id, ...data }) | |
}) | |
const rsa = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }) | |
const ec = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' }) | |
const ed = crypto.generateKeyPairSync('ed25519') | |
const sk = crypto.createSecretKey(crypto.randomBytes(32)) | |
const Benchmark = require('benchmark') | |
const suite = new Benchmark.Suite() | |
;[sk, rsa.privateKey, rsa.publicKey, ec.privateKey, ec.publicKey, ed.privateKey, ed.publicKey].forEach((key) => { | |
suite.add(`${key.type} ${key.asymmetricKeyType || ''} transfer`, { | |
defer: true, | |
fn: function (deferred) { | |
send({ ko: key }).then(deferred.resolve.bind(deferred)) | |
} | |
}) | |
const exportOptions = {} | |
let prop | |
switch (key.type) { | |
case 'private': | |
exportOptions.type = 'pkcs8' | |
exportOptions.format = 'der' | |
;({ type: prop } = exportOptions) | |
break | |
case 'public': | |
exportOptions.type = 'spki' | |
exportOptions.format = 'der' | |
;({ type: prop } = exportOptions) | |
break | |
case 'secret': | |
prop = 'buffer' | |
break | |
} | |
suite.add(`${key.type} ${key.asymmetricKeyType || ''} ${exportOptions.format || ''} export-import`, { | |
defer: true, | |
fn: function (deferred) { | |
send({ [prop]: key.export(exportOptions) }).then(deferred.resolve.bind(deferred)) | |
} | |
}) | |
}) | |
suite.on('cycle', (event) => console.log(String(event.target))) | |
suite.run({ async: true }) | |
} else { | |
parentPort.on('message', ({ id, ...msg }) => { | |
if ('spki' in msg) { | |
crypto.createPublicKey({ key: msg.spki, format: 'der', type: 'spki' }) | |
} else if ('pkcs8' in msg) { | |
crypto.createPrivateKey({ key: msg.pkcs8, format: 'der', type: 'pkcs8' }) | |
} else if ('buffer' in msg) { | |
crypto.createSecretKey(msg.buffer) | |
} | |
parentPort.postMessage({ id }) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment