|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +const Benchmark = require('benchmark'); |
| 3 | +const crypto = require('node:crypto'); |
| 4 | +const utils = require('..'); |
| 5 | + |
| 6 | +const suite = new Benchmark.Suite(); |
| 7 | + |
| 8 | +function createHashWithSHA512(s) { |
| 9 | + const sum = crypto.createHash('sha512'); |
| 10 | + sum.update(s); |
| 11 | + return sum.digest('hex'); |
| 12 | +} |
| 13 | + |
| 14 | +console.log("utils.sha512({foo: 'bar', bar: 'foo', v: [1, 2, 3]})", utils.sha512({ foo: 'bar', bar: 'foo', v: [ 1, 2, 3 ] })); |
| 15 | +console.log("utils.sha512(JSON.stringify({foo: 'bar', bar: 'foo', v: [1, 2, 3]}))", |
| 16 | + utils.sha512(JSON.stringify({ foo: 'bar', bar: 'foo', v: [ 1, 2, 3 ] }))); |
| 17 | +console.log("utils.sha512('苏千')", utils.sha512('苏千')); |
| 18 | + |
| 19 | +console.log('------------- %s -----------', Date()); |
| 20 | + |
| 21 | +suite |
| 22 | + .add("utils.sha512({foo: 'bar', bar: 'foo', v: [1, 2, 3]})", function() { |
| 23 | + utils.sha512({ foo: 'bar', bar: 'foo', v: [ 1, 2, 3 ] }); |
| 24 | + }) |
| 25 | + .add("utils.sha512(JSON.stringify({foo: 'bar', bar: 'foo', v: [1, 2, 3]})))", function() { |
| 26 | + utils.sha512(JSON.stringify({ foo: 'bar', bar: 'foo', v: [ 1, 2, 3 ] })); |
| 27 | + }) |
| 28 | + .add("utils.sha512('苏千')", function() { |
| 29 | + utils.sha512('苏千'); |
| 30 | + }); |
| 31 | + |
| 32 | +if (crypto.hash) { |
| 33 | + suite.add('createHashWithSHA512(Buffer.alloc(1024))', function() { |
| 34 | + createHashWithSHA512(Buffer.alloc(1024)); |
| 35 | + }).add("crypto.hash('sha512', Buffer.alloc(1024))", function() { |
| 36 | + crypto.hash('sha512', Buffer.alloc(1024)); |
| 37 | + }); |
| 38 | + console.log("crypto.hash('sha512', Buffer.alloc(1024))", crypto.hash('sha512', Buffer.alloc(1024))); |
| 39 | + console.log('createHashWithSHA512(Buffer.alloc(1024))', createHashWithSHA512(Buffer.alloc(1024))); |
| 40 | +} |
| 41 | + |
| 42 | +suite |
| 43 | + |
| 44 | +// add listeners |
| 45 | + .on('cycle', function(event) { |
| 46 | + console.log(String(event.target)); |
| 47 | + }) |
| 48 | + .on('complete', function() { |
| 49 | + console.log('Fastest is ' + this.filter('fastest').map('name')); |
| 50 | + }) |
| 51 | + .run({ async: false }); |
| 52 | + |
| 53 | +// node benchmark/sha512.cjs |
| 54 | +// utils.sha512({foo: 'bar', bar: 'foo', v: [1, 2, 3]}) 81e725c5a3e77365521c0f7448e2099d7400b92e8893230495b2ae54d7bb938c063a575ad7cb79750b45f59824a9ff0b251f9d0ba27cadcff0cda8f745538950 |
| 55 | +// utils.sha512(JSON.stringify({foo: 'bar', bar: 'foo', v: [1, 2, 3]})) 1f8288664f4ead755b2e6b5a6b4c4fdfd8fb4933fa398524461f598e22e402af7ae9b49e9473c9cbeb036abbe6e6c6ab3f8484f3d15acc79beaf8aecc0a9b076 |
| 56 | +// utils.sha512('苏千') 913e9b219f70541725a6ed721b42ae88e79f7ea1c7aec53be80ab277d4704b556df265cc4235f942f9dfbbbbd88e02ba2e18f60b217853835aeb362fb1830016 |
| 57 | +// ------------- Wed Mar 13 2024 10:27:21 GMT+0800 (中国标准时间) ----------- |
| 58 | +// crypto.hash('sha512', Buffer.alloc(1024)) 8efb4f73c5655351c444eb109230c556d39e2c7624e9c11abc9e3fb4b9b9254218cc5085b454a9698d085cfa92198491f07a723be4574adc70617b73eb0b6461 |
| 59 | +// createHashWithSHA512(Buffer.alloc(1024)) 8efb4f73c5655351c444eb109230c556d39e2c7624e9c11abc9e3fb4b9b9254218cc5085b454a9698d085cfa92198491f07a723be4574adc70617b73eb0b6461 |
| 60 | +// utils.sha512({foo: 'bar', bar: 'foo', v: [1, 2, 3]}) x 1,169,875 ops/sec ±6.92% (95 runs sampled) |
| 61 | +// utils.sha512(JSON.stringify({foo: 'bar', bar: 'foo', v: [1, 2, 3]}))) x 1,742,893 ops/sec ±1.56% (98 runs sampled) |
| 62 | +// utils.sha512('苏千') x 3,102,952 ops/sec ±1.09% (97 runs sampled) |
| 63 | +// createHashWithSHA512(Buffer.alloc(1024)) x 597,443 ops/sec ±1.08% (90 runs sampled) |
| 64 | +// crypto.hash('sha512', Buffer.alloc(1024)) x 796,968 ops/sec ±0.59% (96 runs sampled) |
| 65 | +// Fastest is utils.sha512('苏千') |
0 commit comments