Use process.nextTick if available to reduce stack size.

This commit is contained in:
Bryce Gibson 2017-01-03 19:04:01 +11:00
parent 2f73e9d01d
commit 1dec7eb3f6

View file

@ -1,7 +1,7 @@
"use strict"
module.exports = new function init() {
var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, hasProcess = typeof process === "object"
var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object"
function o(subject, predicate) {
ctx[subject] = predicate
@ -67,7 +67,6 @@ module.exports = new function init() {
next()
function next() {
stack++
if (cursor === fns.length) return
var fn = fns[cursor++]
@ -104,8 +103,7 @@ module.exports = new function init() {
}
else {
fn()
if (stack < 5000) next()
else (hasProcess ? process.nextTick : setTimeout)(next, stack = 0)
nextTickish(next)
}
}
}
@ -211,5 +209,14 @@ module.exports = new function init() {
if (hasProcess && status === 1) process.exit(1)
}
if(hasProcess) {
nextTickish = process.nextTick
} else {
nextTickish = function fakeFastNextTick(fn) {
if (stack++ < 5000) next()
else setTimeout(next, stack = 0)
}
}
return o
}