benchmarks.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var base62 = require('../'),
  3. now = 0,
  4. deltaTime = 0,
  5. i = 0,
  6. intResult = 0,
  7. strResult = 0;
  8. function performanceNow(){
  9. var t = process.hrtime();
  10. return t[0] * 1000 + t[1] / 1000000;
  11. }
  12. //decode with default charset
  13. now = performanceNow();
  14. for (intResult = 0, i = 0; i < 1000000; i++) {
  15. intResult += base62.decode('00thing');
  16. }
  17. deltaTime = performanceNow() - now;
  18. console.log('|', 'decoding with default charset (1000000x)', '|', intResult === 432635954000000 ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|');
  19. //encode with default charset
  20. now = performanceNow();
  21. for (strResult = '', i = 0; i < 1000000; i++) {
  22. strResult = base62.encode(i);
  23. }
  24. deltaTime = performanceNow() - now;
  25. console.log('|', 'encoding with default charset (1000000x)', '|', strResult === '4c91' ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|');
  26. //decode with custom charset
  27. base62.setCharacterSet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
  28. now = performanceNow();
  29. for (intResult = 0, i = 0; i < 1000000; i++) {
  30. intResult += base62.decode('00thing');
  31. }
  32. deltaTime = performanceNow() - now;
  33. console.log('|', 'decoding with custom charset (1000000x)', '|', intResult === 823118800000000 ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|');
  34. //encode with custom charset
  35. now = performanceNow();
  36. for (strResult = '', i = 0; i < 1000000; i++) {
  37. strResult = base62.encode(i);
  38. }
  39. deltaTime = performanceNow() - now;
  40. console.log('|', 'encoding with custom charset (1000000x)', '|', strResult === '4C91' ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|');