@nxtedition/timers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +111 -0
- package/lib/index.bench.d.ts +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +100 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) nxtedition
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# @nxtedition/timers
|
|
2
|
+
|
|
3
|
+
Optimized timer pooling for Node.js. Delays >= 1000ms are batched into a single 500ms-resolution interval, reducing the number of active handles and improving performance when many long-lived timers are in use.
|
|
4
|
+
|
|
5
|
+
Short delays (< 1000ms) fall through to the native `setTimeout` for full resolution.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @nxtedition/timers
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { setTimeout, clearTimeout } from '@nxtedition/timers'
|
|
17
|
+
|
|
18
|
+
// Short delays use native setTimeout (full resolution)
|
|
19
|
+
const handle = setTimeout(
|
|
20
|
+
(data) => {
|
|
21
|
+
console.log(data)
|
|
22
|
+
},
|
|
23
|
+
50,
|
|
24
|
+
'hello',
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
// Long delays use pooled timers (500ms resolution, lower overhead)
|
|
28
|
+
const pooled = setTimeout(
|
|
29
|
+
(data) => {
|
|
30
|
+
console.log(data)
|
|
31
|
+
},
|
|
32
|
+
5000,
|
|
33
|
+
'world',
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
// Clear works for both
|
|
37
|
+
clearTimeout(handle)
|
|
38
|
+
clearTimeout(pooled)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Opaque data
|
|
42
|
+
|
|
43
|
+
The third argument is passed directly to the callback, avoiding closure allocations:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
setTimeout(
|
|
47
|
+
(ctx) => {
|
|
48
|
+
ctx.connection.write(ctx.payload)
|
|
49
|
+
},
|
|
50
|
+
5000,
|
|
51
|
+
{ connection, payload },
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Refreshing timers
|
|
56
|
+
|
|
57
|
+
Pooled timers (delay >= 1000ms) support `.refresh()` to reset the delay without creating a new timer:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
const handle = setTimeout(
|
|
61
|
+
() => {
|
|
62
|
+
console.log('idle timeout')
|
|
63
|
+
},
|
|
64
|
+
30000,
|
|
65
|
+
undefined,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
// On activity, reset the timer
|
|
69
|
+
handle.refresh()
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### `setTimeout(callback, delay, opaque)`
|
|
75
|
+
|
|
76
|
+
Schedule `callback(opaque)` after `delay` milliseconds.
|
|
77
|
+
|
|
78
|
+
- `delay < 1000` — delegates to native `globalThis.setTimeout`
|
|
79
|
+
- `delay >= 1000` — uses the pooled timer mechanism (500ms resolution)
|
|
80
|
+
|
|
81
|
+
Returns a handle that can be passed to `clearTimeout`.
|
|
82
|
+
|
|
83
|
+
### `clearTimeout(handle)`
|
|
84
|
+
|
|
85
|
+
Cancel a pending timer. Works with both native and pooled handles. Passing `null` or `undefined` is a no-op.
|
|
86
|
+
|
|
87
|
+
## How it works
|
|
88
|
+
|
|
89
|
+
All pooled timers share a single `setTimeout` handle that fires every 500ms. On each tick, pending timers are checked and expired ones are fired. This means pooled timers have at most ~500ms of jitter but dramatically reduce the number of active OS timer handles when many long-lived timers are in use (e.g. connection idle timeouts, retry delays).
|
|
90
|
+
|
|
91
|
+
The internal interval is `unref()`'d so it does not keep the process alive.
|
|
92
|
+
|
|
93
|
+
## Benchmark
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
yarn benchmark
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Results on Apple M3 Pro / Node.js 25.3.0:
|
|
100
|
+
|
|
101
|
+
| Benchmark | native setTimeout | pooled setTimeout | speedup |
|
|
102
|
+
| ------------------------------ | ----------------: | ----------------: | ------: |
|
|
103
|
+
| create + clear | 250.91 ns | 58.90 ns | 4.3x |
|
|
104
|
+
| create + clear (GC) | 1.95 µs | 73.99 ns | 26.4x |
|
|
105
|
+
| batch create + clear 100x (GC) | 41.84 µs | 9.80 µs | 4.3x |
|
|
106
|
+
|
|
107
|
+
Pooled timers avoid native OS timer handles, which are expensive C++ objects for the GC to trace. The advantage grows under GC pressure — up to **26x** faster when GC is forced per iteration.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare class Timeout {
|
|
2
|
+
callback: (opaque: unknown) => void;
|
|
3
|
+
delay: number;
|
|
4
|
+
opaque: unknown;
|
|
5
|
+
state: number;
|
|
6
|
+
constructor(callback: (opaque: unknown) => void, delay: number, opaque: unknown);
|
|
7
|
+
refresh(): void;
|
|
8
|
+
clear(): void;
|
|
9
|
+
}
|
|
10
|
+
export declare function setTimeout<T>(callback: (opaque: T) => void, delay: number, opaque: T): Timeout | ReturnType<typeof globalThis.setTimeout>;
|
|
11
|
+
export declare function clearTimeout(timeout: Timeout | ReturnType<typeof globalThis.setTimeout> | null | undefined): void;
|
|
12
|
+
export {};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
let fastNow = 0
|
|
2
|
+
let fastNowTimeout = null
|
|
3
|
+
|
|
4
|
+
const fastTimers = []
|
|
5
|
+
|
|
6
|
+
function onTimeout() {
|
|
7
|
+
fastNow += 500
|
|
8
|
+
|
|
9
|
+
let len = fastTimers.length
|
|
10
|
+
let idx = 0
|
|
11
|
+
while (idx < len) {
|
|
12
|
+
const timer = fastTimers[idx]
|
|
13
|
+
|
|
14
|
+
if (timer.state === 0) {
|
|
15
|
+
timer.state = fastNow + timer.delay
|
|
16
|
+
} else if (timer.state > 0 && fastNow >= timer.state) {
|
|
17
|
+
timer.state = -1
|
|
18
|
+
timer.callback(timer.opaque)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (timer.state === -1) {
|
|
22
|
+
timer.state = -2
|
|
23
|
+
if (idx !== len - 1) {
|
|
24
|
+
fastTimers[idx] = fastTimers.pop()
|
|
25
|
+
} else {
|
|
26
|
+
fastTimers.pop()
|
|
27
|
+
}
|
|
28
|
+
len -= 1
|
|
29
|
+
} else {
|
|
30
|
+
idx += 1
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (fastTimers.length > 0) {
|
|
35
|
+
refreshTimeout()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function refreshTimeout() {
|
|
40
|
+
if (fastNowTimeout) {
|
|
41
|
+
fastNowTimeout.refresh()
|
|
42
|
+
} else {
|
|
43
|
+
fastNowTimeout = globalThis.setTimeout(onTimeout, 500)
|
|
44
|
+
fastNowTimeout.unref()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class Timeout {
|
|
49
|
+
callback
|
|
50
|
+
delay
|
|
51
|
+
opaque
|
|
52
|
+
|
|
53
|
+
// -2 not in timer list
|
|
54
|
+
// -1 in timer list but inactive
|
|
55
|
+
// 0 in timer list waiting for time
|
|
56
|
+
// > 0 in timer list waiting for time to expire
|
|
57
|
+
state = -2
|
|
58
|
+
|
|
59
|
+
constructor(callback , delay , opaque ) {
|
|
60
|
+
this.callback = callback
|
|
61
|
+
this.delay = delay
|
|
62
|
+
this.opaque = opaque
|
|
63
|
+
this.refresh()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
refresh() {
|
|
67
|
+
if (this.state === -2) {
|
|
68
|
+
fastTimers.push(this)
|
|
69
|
+
if (!fastNowTimeout || fastTimers.length === 1) {
|
|
70
|
+
refreshTimeout()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.state = 0
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
clear() {
|
|
78
|
+
this.state = -1
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function setTimeout (
|
|
83
|
+
callback ,
|
|
84
|
+
delay ,
|
|
85
|
+
opaque ,
|
|
86
|
+
) {
|
|
87
|
+
return delay < 1e3
|
|
88
|
+
? globalThis.setTimeout(callback, delay, opaque)
|
|
89
|
+
: new Timeout(callback , delay, opaque)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function clearTimeout(
|
|
93
|
+
timeout ,
|
|
94
|
+
) {
|
|
95
|
+
if (timeout instanceof Timeout) {
|
|
96
|
+
timeout.clear()
|
|
97
|
+
} else if (timeout != null) {
|
|
98
|
+
globalThis.clearTimeout(timeout)
|
|
99
|
+
}
|
|
100
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nxtedition/timers",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "rimraf lib && tsc && amaroc ./src/index.ts && mv src/index.js lib/",
|
|
18
|
+
"prepublishOnly": "yarn build",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"test": "node --test",
|
|
21
|
+
"test:ci": "node --test",
|
|
22
|
+
"benchmark": "node --experimental-strip-types src/index.bench.ts"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^25.2.3",
|
|
26
|
+
"amaroc": "^1.0.1",
|
|
27
|
+
"mitata": "^1.0.34",
|
|
28
|
+
"oxlint-tsgolint": "^0.12.2",
|
|
29
|
+
"rimraf": "^6.1.2",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "12c38fec15bd155797cb1edae372a81061715df3"
|
|
33
|
+
}
|