npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

antifreeze2

v0.0.2

Published

Tiny helper to prevent the event loop from getting stuck in asynchronous routines

Downloads

22

Readme

antifreeze2

Build Status Coverage Status

:star: Antifreeze for eventloop- let it always work :star:

Why

If you have a heavy synchronous task, you may use workers or just split the task to several async micro tasks/chunks to keep event loop always running, its pretty easy to do that with async function. But async function doesn't guarantee that all of its task scheduled by await will be executed really asynchronously and will not block the IO stage of the EventLoop. This simple package consists some helpers to ensure that the event loop is running, measuring the duration of the current event tick and allowing it go to the next tick if the maximum tick duration is exceeded.

Installation

Install for node.js using npm/yarn:

$ npm install antifreeze2 --save
$ yarn add antifreeze2
const { antifreeze, isNeeded, watchTick }= require('antifreeze2');

Usage examples

Example 1 - Fibonacci

For example, we need to calculate Fibonacci for 1,000,000 value. It's a task with heavy computation since it can take around 10s to complete. If we write the function as synchronous or just use an ECMA asynchronous function, the event loop will be blocked for that period. We won't be able to perform other tasks like accepting new connections, I/O events, timers, etc. because we only have one thread. To avoid this, we must ensure that the event loop tick duration does not exceed the allowed range of 15-20ms in order for the application to remain responsive.

By default, the desired event loop tick is set to 10ms. You can change it using watchTick(maxTick: number) function. See online demo

import {antifreeze, isNeeded} from "antifreeze2";

// A function with heavy computations
const fibAsync = async(n) => {
  let a = 1n, b = 1n, sum, i = n - 2;
  while (i-- > 0) {
    sum = a + b;
    a = b;
    b = sum;
    if (isNeeded()) {      // If more than 10ms have passed since the last run of the eventloop cycle
      await antifreeze();  // let the event loop get polled
    }
  }
  return b;
};

// Test it - calculate Fibonacci for n= 1,000,000
(async (n) => {
  let ts = Date.now();
  let ticks = 0;

  const timer = setInterval(() => {
    const now = Date.now();
    console.log(`Timer tick [${now - ts}ms]`);
    ts = now;
    ticks++;
  }, 100);

  const result = await fibAsync(n);

  console.warn(`\nTimer ticks: ${ticks}\nFibonacci(${n}) = ${result}`)

  clearTimeout(timer);
})(500000);

Optionally, to get the maximum performance, you can throttle the isNeeded() call by using some counter:

const fibAsync = async(n) => {
  let a = 1n, b = 1n, sum, i = n - 2;
  while (i-- > 0) {
    sum = a + b;
    a = b;
    b = sum;
    // check only every 1000th cycle
    if (!(i % 1000) && isNeeded()) {      // If more than 10ms have passed since the last run of the eventloop cycle
      await antifreeze();  // let the event loop get polled
    }
  }
  return b;
};

Example 2 - koa server with heavy computation

See online demo

The application has two endpoints:

Time request - light query with 20ms latency

Fibonacci request - heavy query that takes 10s to complete

Note that while a heavy request is being executed, the server continues to process light requests even though it is only running in one thread.

API

antifreeze2

antifreeze2.watchTick(tick)

set interval for EventLoop delay checking

Kind: static method of antifreeze2

| Param | Type | Description | | --- | --- | --- | | tick | Number | checking interval. Set to 0 to disable the watcher. By default this value is set to 15(ms) |

antifreeze2.antifreeze() ⇒ Promise.<any> | null

Antifreeze promise injector

Kind: static method of antifreeze2

antifreeze2.isNeeded([maxTick]) ⇒ boolean

returns true if current event loop tick is delayed

Kind: static method of antifreeze2

| Param | Description | | --- | --- | | [maxTick] | max tick duration allowed |

Contribution

Feel free to fork, open issues, enhance or create pull requests.

License

The MIT License Copyright (c) 2019 Dmitriy Mozgovoy [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.