Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let scheduleNext
- if (typeof setImmediate != 'undefined') {
- if (!process.nextTick) {
- let npending = 0
- const reset = () => { npending = 0 }
- scheduleNext = function scheduleNext(callback) {
- if (npending == 0) {
- process.nextTick(() => {
- callback()
- process.nextTick(reset)
- })
- ++npending
- } else if (npending < 4) {
- process.nextTick(callback)
- ++npending
- } else {
- setImmediate(callback)
- }
- }
- } else {
- scheduleNext = setImmediate
- }
- } else {
- scheduleNext = process.nextTick.bind(process)
- }
- const NS_PER_SEC = 1e9
- const NS_PER_MS = 1e6
- function sched(label /*, ...cbs :()=>void[] */) {
- const cbs = Array.prototype.slice.call(arguments, 1)
- console.log(`scheduleNext "${label}"`)
- const startTime = process.hrtime()
- scheduleNext(() => {
- const diff = process.hrtime(startTime)
- const duration = ((diff[0] * 1000) + diff[1]) / NS_PER_MS
- console.log(`callback "${label}": ${duration.toFixed(3)} ms`)
- if (cbs) {
- for (const cb of cbs) { cb() }
- }
- })
- }
- sched('1')
- sched('2',
- sched.bind(null, '2.1',
- sched.bind(null, '2.1.1'),
- sched.bind(null, '2.1.2'),
- sched.bind(null, '2.1.3')))
- sched('3',
- sched.bind(null, '3.1'),
- sched.bind(null, '3.2',
- sched.bind(null, '3.2.1'),
- sched.bind(null, '3.2.2'),
- sched.bind(null, '3.2.3')))
- sched('4')
- sched('5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement