Code Monkey home page Code Monkey logo

event-loop-spinner's Introduction

event-loop-spinner

Tiny helper to prevent blocking Node.js event loop.

Example

import { eventLoopSpinner } from 'event-loop-spinner';
async function cpuIntensiveOperationHandler(hugeArray) {
  for (const item of hugeArray) {
    // ...
    if (eventLoopSpinner.isStarving()) {
      await eventLoopSpinner.spin();
    }
  }
}

Reading

Node.js: How even quick async functions can block the Event-Loop, starve I/O

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.