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

weaker

v0.0.3

Published

Weaker maps by value, and enumerable weaker sets

Downloads

3

Readme

Weaker.js

CI test status

WeakerMaps are enumerable Maps with with weak values rather than keys.

WeakerSet are like WeakSets, but enumerable, and clearable.

Install

Both

npm install weaker
const WeakerMap = require('weaker/WeakerMap');
const WeakerSet = require('weaker/WeakerSet');

One or the other

npm install weakermap
npm install weakerset
const WeakerMap = require('weakermap');
const WeakerSet = require('weakerset');

About

Docs

WeakerMap

WeakerSet

WeakerMap

This class implements a pattern similar to the 'WeakMap', however its values are weakly referenced, rather than the keys. This allows for enumeration, clearing and arbitrarily valued keys, with the limitation that the values must be objects. Note that elements may not be garbage collected immediately upon leaving a given scope, however this should not have an impact on memory, since the memory will not be freed until the garbage collector runs, with or without the WeakerMap. See MDN's notes on WeakRefs for more info.

WeakerSet

This class implements a pattern similar to the WeakSet, but allows for enumeration and clearing. Ironically, the implementation depends on a WeakerMap and a WeakMap in tandem, managing a group of WeakRefs. Note that elements may not be garbage collected immediately upon leaving a given scope, however this should not have an impact on memory, since the memory will not be freed until the garbage collector runs, with or without the WeakerSet. See MDN's notes on WeakRefs for more info.

Examples

WeakerMap

A WeakerMap will only hold onto its values as long as they aren't garbage collected. Once that happens they will be removed without any furter intervention from the programmer.

NOTE: The following example makes use of global.gc() to force garbage collection to run regardless of existing heuristics. This requires node to be run with the --expose-gc flag. This is not necessary except to demonstrate the behavior in a short script, where the garbage collector would not normally run until the program exits.

const WeakerMap = require('weaker/WeakerMap');

const wm = new WeakerMap;
const retain  = [];

{
	let a = {a:1}, b = {b:2}, c = {c:3};

	[ ['a', a], ['b', b], ['c', c] ].forEach(e => wm.set(...e));

	retain.push(b,c);
};

const printRemaining = () => {
	retain;       // keep refs in-scope
	global.gc();  // force the garbage collector
	console.log(wm.values());
};

printRemaining();
// The garbage collector hasn't run yet,
// So we still have all three refs
// [ { a: 1 }, { b: 2 }, { c: 3 } ]

setTimeout(printRemaining, 500);
// Once we swap to a new 'job', it can run
// and we only have two objects now:
// [ { b: 2 }, { c: 3 } ]

WeakerSet

A WeakerSet will only hold onto its values as long as they aren't garbage collected. Once that happens they will be removed without any furter intervention from the programmer.

NOTE: The following example makes use of global.gc() to force garbage collection to run regardless of existing heuristics. This requires node to be run with the --expose-gc flag. This is not necessary except to demonstrate the behavior in a short script, where the garbage collector would not normally run until the program exits.

const WeakerSet = require('weaker/WeakerSet');

const ws = new WeakerSet;
const retain  = [];

{
	let a = {a:1}, b = {b:2}, c = {c:3};

	[ a, b, c ].forEach(e => ws.add(e));

	retain.push(b,c);
};

const printRemaining = () => {
	retain;       // keep refs in-scope
	global.gc();  // force the garbage collector
	console.log(ws.values());
};

printRemaining();
// The garbage collector hasn't run yet,
// So we still have all three refs
// [ { a: 1 }, { b: 2 }, { c: 3 } ]

setTimeout(printRemaining, 500);
// Once we swap to a new 'job', it can run
// and we only have two objects now:
// [ { b: 2 }, { c: 3 } ]

Testing

npm test