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

zone-polyfill

v0.0.10

Published

A Zone polyfill for node.js

Downloads

26

Readme

zone-polyfill

npm Version

This is an attempt to create a Zone polyfill for the TC39 proposal at https://github.com/domenic/zones.

I'm aware that there is an angular/zone.js implementation, but:

  1. I found it to not be compliant to the spec
  2. It tries to patch too much.
    This repo patches only basic node Apis: Promise, EventEmitter (maybe more in the future). As the spec says, other libraries should support Zone by themselves, or be passed a zone.wrap(...)
  3. At the time of writing this, angular/zone.js does not support async/await. It looses context.
  4. Here I only try to support Node.js, although if there will be a need, it could be adjusted for other platforms as well.

Notes:

  • There was an experimental (src/zone.node12.js) version for node 12.x, which counts on the latest V8 upgrades to properly keep stack traces through await calls.
    That version proved to be very slow due to forcing V8 to generate stack traces which has its cost.
  • I actually took some of the tests from the angular repo, as they already have solid tests (mainly for EventEmitter).

Benchmarks of zone.js vs zone-polyfill (2019-11-12, Intel i7-6770HQ @ 2.60GHz):

  • zone.js - Zone-less code - 1062717 ops/sec
  • zone.js - Zoned code - 892525 ops/sec - slower by 19.07% than Zone-less code
  • zone-polyfill - Zone-less code - 3084971 ops/sec
  • zone-polyfill - Zoned code - 2472462 ops/sec - slower by 24.77% than Zone-less code
  • No require of any Zone library - 3470300 ops/sec
  • zone-polyfill vs zone-js - Zoned code - zone-polyfill faster by 277%
  • zone-polyfill vs zone-js - Zone-less code - zone-polyfill faster by 190%
  • Penalty of requiring zone-polyfill, without any usage, vs clean code - 12%
  • Zone-less mean that the Zone library is required, but fork/run not called in the benchmark unit.
  • Zone mean that the Zone library is required, and fork/run are called in the benchmark unit.

These benchmarks are testing a very simple case of usage vs non-usage of Zone methods, to measure basic penalty to code performance.
There could be made more complex benchmarks, for longer scenarios, but these are enough for me to know not to use zone.js.

Installation:

npm i zone-polyfill

// Vanilla Zone implementation, no default patches:
const Zone = require('zone-polyfill');

// Patch the basic stuff:
const Zone = require('zone-polyfill/patches').patch();

// Or, selectively:
const Zone = require('zone-polyfill/patches/events').patch();
const Zone = require('zone-polyfill/patches/promise').patch();
const Zone = require('zone-polyfill/patches/timers').patch();
const Zone = require('zone-polyfill/patches/nextTick').patch();

zone-polyfill provides these (opt-in) patches:

  • EventEmitter
  • Promise
  • setInterval
  • setTimeout
  • setImmediate
  • process.nextTick

This covers most cases, as many callbacks in various libraries are triggered from events etc.
This will not cover more complicated cases like a library that has one long-running background connection and many library endpoints that listen to it, as the events were not bound in the context of the current Zone.
In this case - Zone.current.wrap/Zone.current.run usage is required, or a manual patch.

An example of a manual patch:

function patchCallbackArg(proto, name) {
    const original = proto[name];
    proto[name] = function (...args) {
        let cb = args[args.length - 1];
        if (typeof cb === 'function') {
            args[args.length - 1] = Zone.current.wrap(cb);
        }
        return original.apply(this, args);
    };
}

patchCallbackArg(Mongodb.Collection.prototype, 'find');