event-loop-spinner
Advanced tools
Comparing version 2.1.0 to 2.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EventLoopSpinner = void 0; | ||
const immediately = typeof setImmediate === 'function' | ||
? setImmediate | ||
: (cb) => cb(); | ||
class EventLoopSpinner { | ||
@@ -13,3 +16,3 @@ constructor(thresholdMs = 10) { | ||
async spin() { | ||
return new Promise((resolve) => setImmediate(() => { | ||
return new Promise((resolve) => immediately(() => { | ||
this.afterLastSpin = Date.now(); | ||
@@ -16,0 +19,0 @@ resolve(); |
@@ -0,1 +1,6 @@ | ||
const immediately = | ||
typeof setImmediate === 'function' | ||
? setImmediate | ||
: (cb: () => void): void => cb(); | ||
export class EventLoopSpinner { | ||
@@ -14,3 +19,3 @@ private afterLastSpin: number; | ||
return new Promise((resolve) => | ||
setImmediate(() => { | ||
immediately(() => { | ||
this.afterLastSpin = Date.now(); | ||
@@ -17,0 +22,0 @@ resolve(); |
{ | ||
"name": "event-loop-spinner", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Tiny helper to prevent blocking Node.js event loop", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -19,4 +19,28 @@ # event-loop-spinner | ||
## Reading | ||
[Node.js: How even quick async functions can block the Event-Loop, starve I/O](https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/) | ||
## How does this help? | ||
Node can only run one bit of JS at a time. While it is running this bit of JS, it does nothing else. | ||
It doesn't fire timers. It doesn't accept TCP connections from clients. | ||
In fact, Node doesn't even do these things when it finishes running one bit of JS. It prefers to | ||
immediately run the next bit of JS. It does this for "a while". | ||
This gives you two amazingly powerful ways to shoot yourself in the foot; it's great! Just like C++. | ||
That is, if some node process either: | ||
* contains some JS code which runs for "too long", or | ||
* handles too many concurrent work items, resulting in lots of JS to run, | ||
..then it won't handle network requests, or let anyone else do any JS. This can result in the | ||
application appearing unresponsive for seconds or minutes. (Yes, we really see minutes.) | ||
`event-loop-spinner` masks this problem. The `spin()` method allows Node to stop running this JS | ||
function, and its friends, and do some other JS, or do some IO. This significantly shortens the | ||
time before more networking is done (i.e. a response is sent to a client), and/or other people's | ||
JS is run, improving responsiveness. |
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
8332
158
46