benchmark.js 721 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // A quick microbenchmark for OError.tag.
  3. //
  4. const OError = require('..')
  5. function benchmark(fn, repeats = 100000) {
  6. const startTime = process.hrtime()
  7. for (let i = 0; i < repeats; ++i) {
  8. fn()
  9. }
  10. const elapsed = process.hrtime(startTime)
  11. return elapsed[0] * 1e3 + elapsed[1] * 1e-6
  12. }
  13. function throwError() {
  14. throw new Error('here is a test error')
  15. }
  16. console.log(
  17. 'no tagging: ',
  18. benchmark(() => {
  19. try {
  20. throwError()
  21. return 1
  22. } catch (error) {
  23. return 0
  24. }
  25. }),
  26. 'ms'
  27. )
  28. console.log(
  29. 'tagging: ',
  30. benchmark(() => {
  31. try {
  32. throwError()
  33. return 1
  34. } catch (error) {
  35. OError.tag(error, 'here is a test tag')
  36. return 0
  37. }
  38. }),
  39. 'ms'
  40. )