Guest User

Untitled

a guest
Nov 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const benchmark = require('benchmark');
  4.  
  5. const suite = new benchmark.Suite;
  6.  
  7. suite
  8. .add('async / await with one single try / catch', async function() {
  9. async function step1() {
  10. const foo = await step2();
  11. return foo + 1;
  12. }
  13.  
  14. async function step2() {
  15. const foo = await step3();
  16. return foo + 1;
  17. }
  18.  
  19. async function step3() {
  20. let foo = await step4();
  21. // Should handle it here but I can't / won't
  22. return foo + 1;
  23. }
  24.  
  25. async function step4() {
  26. throw new Error('ayy');
  27. }
  28.  
  29. let result;
  30. try {
  31. result = await step1();
  32. } catch (err) {
  33. // Handle it
  34. }
  35. })
  36. .add('async / await with nested try / catch', async function() {
  37. async function step1() {
  38. const foo = await step2();
  39. return foo + 1;
  40. }
  41.  
  42. async function step2() {
  43. const foo = await step3();
  44. return foo + 1;
  45. }
  46.  
  47. async function step3() {
  48. let foo = 0;
  49. try {
  50. foo = await step4();
  51. } catch (err) {
  52. // Recover it
  53. }
  54. return foo + 1;
  55. }
  56.  
  57. async function step4() {
  58. throw new Error('ayy');
  59. }
  60.  
  61. let result;
  62. try {
  63. result = await step1();
  64. } catch (err) {
  65. // Handle it
  66. }
  67. })
  68. .add('Promise.catch', {
  69. defer: true,
  70. fn: function(deferred) {
  71. function step1() {
  72. return step2().then(ok => {
  73. return ok + 1;
  74. })
  75. }
  76.  
  77. function step2() {
  78. return step3().then(ok => {
  79. return ok + 1;
  80. });
  81. }
  82.  
  83. function step3() {
  84. return step4()
  85. .then(ok => {
  86. return ok + 1;
  87. })
  88. .catch(err => {
  89. return 1;
  90. })
  91. }
  92.  
  93. function step4() {
  94. return Promise.reject(new Error('ayy'));
  95. }
  96.  
  97. step1().then(ok => {
  98. deferred.resolve();
  99. }).catch(err => {
  100. // Handle it
  101. });
  102. }
  103. })
  104. // add listeners
  105. .on('cycle', function(event) {
  106. console.log(String(event.target));
  107. })
  108. .on('complete', function() {
  109. console.log('Fastest is ' + this.filter('fastest').map('name'));
  110. })
  111. .run({
  112. async: true
  113. });
Add Comment
Please, Sign In to add comment