From 83934b9d39dd2ab323a47f1ff472e1f42af29d42 Mon Sep 17 00:00:00 2001 From: Martin Bayly Date: Tue, 30 Apr 2019 11:35:25 -0700 Subject: [PATCH] Improve performance of feature_store.get nextTick appears to perform much faster than setTimeout and marginally faster than setImmediate in environments we've tested in. (AWS linux) This nodejs event loop blog post describes when to use setImmediate vs process.nextTick. Although it recommends setImmediate for most cases, in this case, process.nextTick feels more appropriate. https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick-vs-setimmediate The relative performance of process.nextTick, setImmediate and setTimeout can be dependent on the hardware, OS, nodeJS version according to this benchmark post. So we should test in a prod environment to ensure that replacing setTimeout with a more performant variant improves LD performance. https://gist.github.com/mmalecki/1257394 --- feature_store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_store.js b/feature_store.js index 083928e..d4730e8 100644 --- a/feature_store.js +++ b/feature_store.js @@ -18,7 +18,7 @@ function InMemoryFeatureStore() { function callbackResult(cb, result) { cb = cb || noop; - setTimeout(function() { cb(result); }, 0); // ensure this is dispatched asynchronously + process.nextTick(cb.bind(null, result)); // ensure this is dispatched asynchronously } store.get = function(kind, key, cb) {