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

@jaenster/weakrefmap

v0.2.2

Published

WeakRefMap, a weak map with access to the fields, and keys can be primitives

Downloads

5

Readme

WeakMap but completed

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

WeakRefMap.

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

Example

import {WeakRefMap} from "weakrefmap";


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

    doSomething() {

    }
}

class Server {
    clients = new WeakRefMap<string, Client>();
}

function test() {
    const server = new Server();
    let alice = new Client(server);
    const bob = new Client(server);
    server.clients.set('alice', alice).add('bob', bob);

    server.clients.forEach(client => client.doSomething());
    
    alice = null;
    // Alice is still floating in memory
    if (server.clients.has('alice')) {
      const lostAlice = server.clients.get('alice')
    }
}

test();

If this code was written with a normal new Map<string, 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 WeakRefMap 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 map 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 Map?

  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