Comparing version 2.0.0 to 3.0.0
22
bench.js
'use strict' | ||
var bench = require('fastbench') | ||
var retimer = require('./') | ||
var max = 10000 | ||
/* eslint-disable no-var */ | ||
const bench = require('fastbench') | ||
const retimer = require('./') | ||
const max = 10000 | ||
function benchSetTimeout (done) { | ||
var timers = new Array(max) | ||
var completed = 0 | ||
var toReschedule = 20 | ||
const timers = new Array(max) | ||
let completed = 0 | ||
let toReschedule = 20 | ||
@@ -34,5 +36,5 @@ schedule() | ||
function benchRetimer (done) { | ||
var timers = new Array(max) | ||
var completed = 0 | ||
var toReschedule = 20 | ||
const timers = new Array(max) | ||
let completed = 0 | ||
let toReschedule = 20 | ||
@@ -61,3 +63,3 @@ schedule() | ||
var run = bench([ | ||
const run = bench([ | ||
benchSetTimeout, | ||
@@ -64,0 +66,0 @@ benchRetimer |
{ | ||
"name": "retimer", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Reschedulable Timer for your node needs", | ||
@@ -8,3 +8,3 @@ "main": "retimer.js", | ||
"lint": "standard", | ||
"test": "tape test.js | faucet" | ||
"test": "tape test.js | tap-dot" | ||
}, | ||
@@ -33,6 +33,6 @@ "pre-commit": [ | ||
"fastbench": "^1.0.0", | ||
"faucet": "0.0.1", | ||
"pre-commit": "^1.0.10", | ||
"standard": "^12.0.0", | ||
"tape": "^4.0.0" | ||
"standard": "^16.0.0", | ||
"tap-dot": "^2.0.0", | ||
"tape": "^5.0.0" | ||
}, | ||
@@ -39,0 +39,0 @@ "browser": { |
# retimer [![Build Status](https://travis-ci.org/mcollina/retimer.png)](https://travis-ci.org/mcollina/retimer) | ||
reschedulable setTimeout for you node needs. This library is built for | ||
reschedulable setTimeout for your node needs. This library is built for | ||
building a keep alive functionality across a large numbers of | ||
@@ -10,5 +10,6 @@ clients/sockets. | ||
* `benchSetTimeout*100: 36912.680ms` | ||
* `benchRetimer*100: 33213.134ms` | ||
* `benchSetTimeout*100: 40.295s` | ||
* `benchRetimer*100: 36.122s` | ||
## Install | ||
@@ -45,7 +46,5 @@ | ||
Reschedule the timer, if the specified timeout comes __after__ the | ||
original timeout. | ||
Reschedule the timer. | ||
Retimer will not gove any performance benefit if the specified timeout comes __before__ the original timeout. | ||
Returns true if successful, false otherwise | ||
### timer.clear() | ||
@@ -59,3 +58,4 @@ | ||
timers this Linked List becomes massive which makes __removing a timer an expensive operation__. | ||
Retimer let the old timer run at its time, and schedule a new one accordingly. | ||
Retimer let the old timer run at its time, and schedule a new one accordingly, when the new one is __after__ the original timeout. | ||
There is no performance gain when the new timeout is before the original one as retimer will just __remove the previous timer__. | ||
@@ -62,0 +62,0 @@ ## License |
'use strict' | ||
var getTime = require('./time') | ||
const getTime = require('./time') | ||
function Retimer (callback, timeout, args) { | ||
var that = this | ||
class Retimer { | ||
constructor (callback, timeout, args) { | ||
const that = this | ||
this._started = getTime() | ||
this._rescheduled = 0 | ||
this._scheduled = timeout | ||
this._args = args | ||
this._started = getTime() | ||
this._rescheduled = 0 | ||
this._scheduled = timeout | ||
this._args = args | ||
this._triggered = false | ||
this._timer = setTimeout(timerWrapper, timeout) | ||
this._timerWrapper = () => { | ||
if (that._rescheduled > 0) { | ||
that._scheduled = that._rescheduled - (getTime() - that._started) | ||
that._schedule(that._scheduled) | ||
} else { | ||
that._triggered = true | ||
callback.apply(null, that._args) | ||
} | ||
} | ||
function timerWrapper () { | ||
if (that._rescheduled > 0) { | ||
that._scheduled = that._rescheduled - (getTime() - that._started) | ||
that._timer = setTimeout(timerWrapper, that._scheduled) | ||
that._rescheduled = 0 | ||
this._timer = setTimeout(this._timerWrapper, timeout) | ||
} | ||
reschedule (timeout) { | ||
if (!timeout) { | ||
timeout = this._scheduled | ||
} | ||
const now = getTime() | ||
if ((now + timeout) - (this._started + this._scheduled) < 0) { | ||
clearTimeout(this._timer) | ||
this._schedule(timeout) | ||
} else if (!this._triggered) { | ||
this._started = now | ||
this._rescheduled = timeout | ||
} else { | ||
callback.apply(null, that._args) | ||
this._schedule(timeout) | ||
} | ||
} | ||
} | ||
Retimer.prototype.reschedule = function (timeout) { | ||
var now = getTime() | ||
if ((now + timeout) - (this._started + this._scheduled) < 0) { | ||
return false | ||
} else { | ||
this._started = now | ||
this._rescheduled = timeout | ||
return true | ||
_schedule (timeout) { | ||
this._triggered = false | ||
this._started = getTime() | ||
this._rescheduled = 0 | ||
this._scheduled = timeout | ||
this._timer = setTimeout(this._timerWrapper, timeout) | ||
} | ||
} | ||
Retimer.prototype.clear = function () { | ||
clearTimeout(this._timer) | ||
clear () { | ||
clearTimeout(this._timer) | ||
} | ||
} | ||
@@ -50,3 +66,3 @@ | ||
var args | ||
let args | ||
@@ -56,2 +72,3 @@ if (arguments.length > 0) { | ||
/* eslint-disable no-var */ | ||
for (var i = 0; i < args.length; i++) { | ||
@@ -58,0 +75,0 @@ args[i] = arguments[i + 2] |
53
test.js
'use strict' | ||
var test = require('tape') | ||
var retimer = require('./') | ||
const test = require('tape') | ||
const retimer = require('./') | ||
@@ -9,3 +9,3 @@ test('schedule a callback', function (t) { | ||
var start = Date.now() | ||
const start = Date.now() | ||
@@ -18,7 +18,7 @@ retimer(function () { | ||
test('reschedule a callback', function (t) { | ||
t.plan(2) | ||
t.plan(1) | ||
var start = Date.now() | ||
const start = Date.now() | ||
var timer = retimer(function () { | ||
const timer = retimer(function () { | ||
t.ok(Date.now() - start >= 70, 'it was deferred ok!') | ||
@@ -28,3 +28,3 @@ }, 50) | ||
setTimeout(function () { | ||
t.equal(timer.reschedule(50), true, 'returns true') | ||
timer.reschedule(50) | ||
}, 20) | ||
@@ -36,5 +36,5 @@ }) | ||
var start = Date.now() | ||
const start = Date.now() | ||
var timer = retimer(function () { | ||
const timer = retimer(function () { | ||
t.ok(Date.now() - start >= 90, 'it was deferred ok!') | ||
@@ -54,3 +54,3 @@ }, 50) | ||
var timer = retimer(function () { | ||
const timer = retimer(function () { | ||
t.fail('the timer should never get called') | ||
@@ -69,3 +69,3 @@ }, 20) | ||
var timer = retimer(function () { | ||
const timer = retimer(function () { | ||
t.fail('the timer should never get called') | ||
@@ -86,16 +86,33 @@ }, 20) | ||
test('return false if rescheduled too early', function (t) { | ||
t.plan(2) | ||
test('can be rescheduled early', function (t) { | ||
t.plan(1) | ||
var start = Date.now() | ||
const start = Date.now() | ||
var timer = retimer(function () { | ||
t.ok(Date.now() - start >= 50, 'it was deferred ok!') | ||
}, 50) | ||
const timer = retimer(function () { | ||
t.ok(Date.now() - start <= 500, 'it was rescheduled!') | ||
}, 500) | ||
setTimeout(function () { | ||
t.equal(timer.reschedule(10), false, 'return false') | ||
timer.reschedule(10) | ||
}, 20) | ||
}) | ||
test('can be rescheduled even if the timeout has already triggered', function (t) { | ||
t.plan(2) | ||
const start = Date.now() | ||
let count = 0 | ||
const timer = retimer(function () { | ||
count++ | ||
if (count === 1) { | ||
t.ok(Date.now() - start >= 20, 'it was triggered!') | ||
timer.reschedule(20) | ||
} else { | ||
t.ok(Date.now() - start >= 40, 'it was rescheduled!') | ||
} | ||
}, 20) | ||
}) | ||
test('pass arguments to the callback', function (t) { | ||
@@ -102,0 +119,0 @@ t.plan(1) |
'use strict' | ||
module.exports = function getTime () { | ||
var t = process.hrtime() | ||
const t = process.hrtime() | ||
return Math.floor(t[0] * 1000 + t[1] / 1000000) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8877
217