From 610d9234de57fd27294631e7b6c714e44cca05a7 Mon Sep 17 00:00:00 2001 From: Nico Wagner Date: Mon, 25 Sep 2023 23:21:06 +0200 Subject: [PATCH 1/2] fix: add config option to change the setImmediate time usage to improve performance --- lib/nanotimer.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/nanotimer.js b/lib/nanotimer.js index 909721e..12532f9 100644 --- a/lib/nanotimer.js +++ b/lib/nanotimer.js @@ -1,4 +1,10 @@ -function NanoTimer(log){ +/** + * + * @param log {boolean} if logging should be enabled + * @param config.timeUntilUseOfSetImmediateInNanoSeconds = 25_000_000 + * @constructor + */ +function NanoTimer(log = false, config){ var version = process.version; var major = version.split('.')[0]; @@ -37,6 +43,8 @@ function NanoTimer(log){ this.timeoutTriggered = false; + this.timeUntilUseOfSetImmediateInNanoSeconds = config.timeUntilUseOfSetImmediateInNanoSeconds || 25_000_000; + if(log){ this.logging = true; } @@ -193,7 +201,7 @@ NanoTimer.prototype.setInterval = function(task, args, interval, callback){ if(this.difTime < (this.intervalTime*this.intervalCount)){ //Can potentially defer to less accurate setTimeout if intervaltime > 25ms - if(this.intervalTime > 25000000){ + if(this.intervalTime > this.timeUntilUseOfSetImmediateInNanoSeconds){ if(this.deferredInterval == false){ this.deferredInterval = true; var msDelay = (this.intervalTime - 25000000) / 1000000.0; @@ -317,7 +325,7 @@ NanoTimer.prototype.setTimeout = function(task, args, delay, callback){ if(difTime < delayTime){ //Can potentially defer to less accurate setTimeout if delayTime > 25ms - if(delayTime > 25000000){ + if(delayTime > this.timeUntilUseOfSetImmediateInNanoSeconds){ if(this.deferredTimeout == false){ this.deferredTimeout = true; var msDelay = (delayTime - 25000000) / 1000000.0; From 11126999bc0646cb2334fe08b95bc164410866ea Mon Sep 17 00:00:00 2001 From: Nico Wagner Date: Mon, 25 Sep 2023 23:31:15 +0200 Subject: [PATCH 2/2] fix: made config parameter optional --- lib/nanotimer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nanotimer.js b/lib/nanotimer.js index 12532f9..fbfc252 100644 --- a/lib/nanotimer.js +++ b/lib/nanotimer.js @@ -43,7 +43,7 @@ function NanoTimer(log = false, config){ this.timeoutTriggered = false; - this.timeUntilUseOfSetImmediateInNanoSeconds = config.timeUntilUseOfSetImmediateInNanoSeconds || 25_000_000; + this.timeUntilUseOfSetImmediateInNanoSeconds = config?.timeUntilUseOfSetImmediateInNanoSeconds || 25_000_000; if(log){ this.logging = true;