random_string.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**!
  2. * utility - benchmark/random_string.js
  3. *
  4. * Copyright(c) fengmk2 and other contributors.
  5. * MIT Licensed
  6. *
  7. * Authors:
  8. * fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
  9. */
  10. 'use strict';
  11. /**
  12. * Module dependencies.
  13. */
  14. var crypto = require('crypto');
  15. var utility = require('../');
  16. function randomString(length) {
  17. return crypto.randomBytes(length).toString('base64');
  18. }
  19. function pseudoRandomString(length) {
  20. return crypto.pseudoRandomBytes(length).toString('base64');
  21. }
  22. var Benchmark = require('benchmark');
  23. var suite = new Benchmark.Suite();
  24. console.log("crypto.randomString(16): %s", randomString(16));
  25. console.log("crypto.pseudoRandomString(16): %s", pseudoRandomString(16));
  26. console.log("utility.randomString(16): %s", utility.randomString(16));
  27. console.log("crypto.randomString(32): %s", randomString(32));
  28. console.log("crypto.pseudoRandomString(32): %s", pseudoRandomString(32));
  29. console.log("utility.randomString(32): %s", utility.randomString(32));
  30. console.log('------------- %s -----------', Date());
  31. suite
  32. .add("crypto.randomString(16)", function () {
  33. randomString(16);
  34. })
  35. .add("crypto.randomString(32)", function () {
  36. randomString(32);
  37. })
  38. .add("crypto.pseudoRandomString(16)", function () {
  39. pseudoRandomString(16);
  40. })
  41. .add("crypto.pseudoRandomString(32)", function () {
  42. pseudoRandomString(32);
  43. })
  44. .add("utility.randomString(16)", function () {
  45. utility.randomString(16);
  46. })
  47. .add("utility.randomString(32)", function () {
  48. utility.randomString(32);
  49. })
  50. // add listeners
  51. .on('cycle', function (event) {
  52. console.log(String(event.target));
  53. })
  54. .on('complete', function () {
  55. console.log('Fastest is ' + this.filter('fastest').pluck('name'));
  56. })
  57. .run({ async: false });
  58. // $ node benchmark/random_string.js
  59. // crypto.randomString(16): or0FCEq0YuQoHdoHIp3KlQ==
  60. // crypto.pseudoRandomString(16): AD/CWJR3CbUG/fGji1fkkg==
  61. // utility.randomString(16): Rj9O7bbVpxG6AQjQ
  62. // crypto.randomString(32): 94Ny5xa/z1WH0KMYJfkkPum0b8qJ6ZysNmG0xlenYsk=
  63. // crypto.pseudoRandomString(32): RqY4x3Xt2R1uKe0ZLhgXC5OfnRCTG7TPl8JLIPXceek=
  64. // utility.randomString(32): jCQvW3HAXdzV2ohMzbvPhlJCsoPoqRuO
  65. // ------------- Wed May 07 2014 10:18:13 GMT+0800 (CST) -----------
  66. // crypto.randomString(16) x 140,867 ops/sec ±3.91% (77 runs sampled)
  67. // crypto.randomString(32) x 136,991 ops/sec ±3.15% (87 runs sampled)
  68. // crypto.pseudoRandomString(16) x 141,351 ops/sec ±4.21% (85 runs sampled)
  69. // crypto.pseudoRandomString(32) x 131,813 ops/sec ±3.40% (85 runs sampled)
  70. // utility.randomString(16) x 2,081,522 ops/sec ±2.03% (96 runs sampled)
  71. // utility.randomString(32) x 1,110,170 ops/sec ±2.53% (96 runs sampled)
  72. // Fastest is utility.randomString(16)