Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. let scheduleNext
  2.  
  3. if (typeof setImmediate != 'undefined') {
  4. if (!process.nextTick) {
  5. let npending = 0
  6. const reset = () => { npending = 0 }
  7. scheduleNext = function scheduleNext(callback) {
  8. if (npending == 0) {
  9. process.nextTick(() => {
  10. callback()
  11. process.nextTick(reset)
  12. })
  13. ++npending
  14. } else if (npending < 4) {
  15. process.nextTick(callback)
  16. ++npending
  17. } else {
  18. setImmediate(callback)
  19. }
  20. }
  21. } else {
  22. scheduleNext = setImmediate
  23. }
  24. } else {
  25. scheduleNext = process.nextTick.bind(process)
  26. }
  27.  
  28.  
  29. const NS_PER_SEC = 1e9
  30. const NS_PER_MS = 1e6
  31.  
  32. function sched(label /*, ...cbs :()=>void[] */) {
  33. const cbs = Array.prototype.slice.call(arguments, 1)
  34. console.log(`scheduleNext "${label}"`)
  35. const startTime = process.hrtime()
  36. scheduleNext(() => {
  37. const diff = process.hrtime(startTime)
  38. const duration = ((diff[0] * 1000) + diff[1]) / NS_PER_MS
  39. console.log(`callback "${label}": ${duration.toFixed(3)} ms`)
  40. if (cbs) {
  41. for (const cb of cbs) { cb() }
  42. }
  43. })
  44. }
  45.  
  46. sched('1')
  47. sched('2',
  48. sched.bind(null, '2.1',
  49. sched.bind(null, '2.1.1'),
  50. sched.bind(null, '2.1.2'),
  51. sched.bind(null, '2.1.3')))
  52. sched('3',
  53. sched.bind(null, '3.1'),
  54. sched.bind(null, '3.2',
  55. sched.bind(null, '3.2.1'),
  56. sched.bind(null, '3.2.2'),
  57. sched.bind(null, '3.2.3')))
  58. sched('4')
  59. sched('5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement