benchmark.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. try {
  2. var nodeuuid = require('../uuid');
  3. } catch (e) {
  4. console.error('node-uuid require failed - skipping tests');
  5. }
  6. try {
  7. var uuid = require('uuid');
  8. } catch (e) {
  9. console.error('uuid require failed - skipping tests');
  10. }
  11. try {
  12. var uuidjs = require('uuid-js');
  13. } catch (e) {
  14. console.error('uuid-js require failed - skipping tests');
  15. }
  16. var N = 5e5;
  17. function rate(msg, t) {
  18. console.log(msg + ': ' +
  19. (N / (Date.now() - t) * 1e3 | 0) +
  20. ' uuids/second');
  21. }
  22. console.log('# v4');
  23. // node-uuid - string form
  24. if (nodeuuid) {
  25. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4();
  26. rate('nodeuuid.v4() - using node.js crypto RNG', t);
  27. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4({rng: nodeuuid.mathRNG});
  28. rate('nodeuuid.v4() - using Math.random() RNG', t);
  29. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary');
  30. rate('nodeuuid.v4(\'binary\')', t);
  31. var buffer = new nodeuuid.BufferClass(16);
  32. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary', buffer);
  33. rate('nodeuuid.v4(\'binary\', buffer)', t);
  34. }
  35. // libuuid - string form
  36. if (uuid) {
  37. for (var i = 0, t = Date.now(); i < N; i++) uuid();
  38. rate('uuid()', t);
  39. for (var i = 0, t = Date.now(); i < N; i++) uuid('binary');
  40. rate('uuid(\'binary\')', t);
  41. }
  42. // uuid-js - string form
  43. if (uuidjs) {
  44. for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(4);
  45. rate('uuidjs.create(4)', t);
  46. }
  47. // 140byte.es
  48. for (var i = 0, t = Date.now(); i < N; i++) 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(s,r){r=Math.random()*16|0;return (s=='x'?r:r&0x3|0x8).toString(16)});
  49. rate('140byte.es_v4', t);
  50. console.log('');
  51. console.log('# v1');
  52. // node-uuid - v1 string form
  53. if (nodeuuid) {
  54. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1();
  55. rate('nodeuuid.v1()', t);
  56. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary');
  57. rate('nodeuuid.v1(\'binary\')', t);
  58. var buffer = new nodeuuid.BufferClass(16);
  59. for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary', buffer);
  60. rate('nodeuuid.v1(\'binary\', buffer)', t);
  61. }
  62. // uuid-js - v1 string form
  63. if (uuidjs) {
  64. for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(1);
  65. rate('uuidjs.create(1)', t);
  66. }