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

weakrefset

v0.2.2

Published

WeakRefSet, a weak set with access to the fields

Downloads

1

Readme

WeakSet but completed

Weak references are great, but nearly nothing truly uses it in javascript.

WeakRefSet.

Why not just use WeakSet? A native WeakSet is write only, you cant do things like .forEach or for (const value of set). The only thing it can be used for is seeing if it contains it.

Example

import {WeakRefSet} from "weakrefset";


class Clients {
    constructor(private readonly server: Server) {
    }

    doSomething() {

    }
}

class Server {
    clients = new WeakRefSet<Client>();
}

function test() {
    const server = new Server();
    const clientA = new Client(server);
    const clientB = new Client(server);
    server.clients.add(clientA).add(clientB);

    server.clients.forEach(client => client.doSomething());
}

test();

If this code was written with a normal new Set<Client>, everything would float forever in memory and never be cleaned. As both Client as Server have recursive references to each other. The magic of a weak reference will overcome this issue.

In the past, we would have demanded the user to call some server.close() function, but as we (java|type)script developers are lazy and not used with dealing with the garbage collector, we often forget.

With WeakRef the following happen

  1. End of test(), reference all references to the client (clientA, clientB) get marked as deletable. As the variables are the only ones that count. The weak ref set is not counted as a reference
  2. All Client instances get deleted
  3. All server references are gone now, as test()'s server variable is gone, and so are all Client objects are gone
  4. All memory usage of test() is fully and automatically cleaned up

What happens with a typical Set?

  1. End of test(), references to the clientA and clientB are still holden in the Server instance.
  2. clients instances still exists
  3. server reference still exists in clients
  4. all memory of test() is still in memory
  5. memory leaked