From 1dec7eb3f6e4e30445031c74b0ac9dd2fd2f37d5 Mon Sep 17 00:00:00 2001 From: Bryce Gibson Date: Tue, 3 Jan 2017 19:04:01 +1100 Subject: [PATCH] Use process.nextTick if available to reduce stack size. --- ospec/ospec.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index 56111b66..3f9e5ec4 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -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 }