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

@suldashi/weak

v2.0.1

Published

Make weak references to JavaScript Objects. Fixed Node 12 build.

Downloads

6

Readme

node-weak

Make weak references to JavaScript Objects.

Build Status Build Status

On certain rarer occasions, you run into the need to be notified when a JavaScript object is going to be garbage collected. This feature is exposed to V8's C++ API, but not to JavaScript.

That's where node-weak comes in! This module exports V8's Persistent<Object> functionality to JavaScript. This allows you to create weak references, and optionally attach a callback function to any arbitrary JS object. The callback function will be invoked right before the Object is garbage collected (i.e. after there are no more remaining references to the Object in JS-land).

This module can, for example, be used for debugging; to determine whether or not an Object is being garbage collected as it should. Take a look at the example below for commented walkthrough scenario.

Installation

Install with npm:

$ npm install weak

Example

Here's an example of calling a cleanup() function on a Object before it gets garbage collected:

var weak = require('weak')

// we are going to "monitor" this Object and invoke "cleanup"
// before the object is garbage collected
var obj = {
    a: true
  , foo: 'bar'
}

// Here's where we set up the weak reference
var ref = weak(obj, function () {
  // `this` inside the callback is the EventEmitter.
  console.log('"obj" has been garbage collected!')
})

// While `obj` is alive, `ref` proxies everything to it, so:
ref.a   === obj.a
ref.foo === obj.foo

// Clear out any references to the object, so that it will be GC'd at some point...
obj = null

//
//// Time passes, and the garbage collector is run
//

// `callback()` above is called, and `ref` now acts like an empty object.
typeof ref.foo === 'undefined'

Weak Callback Function "Best Practices"

It's important to be careful when using the "callbacks" feature of node-weak, otherwise you can end up in a situation where the watched object will never be garbage collected.

You should not define the callback function in the same scope as the object that is being watched. It's often best to define the callback function at the highest scope possible (top-level being the best). Named functions work really well for this:

var http = require('http')
  , weak = require('weak')

http.createServer(function (req, res) {
  weak(req, gcReq)
  weak(res, gcRes)
  res.end('Hello World\n')
}).listen(3000)

function gcReq () {
  console.log('GC\'d `req` object')
}

function gcRes () {
  console.log('GC\'d `res` object')
}

API

Weakref weak(Object obj [, Function callback])

The main exports is the function that creates the weak reference. The first argument is the Object that should be monitored. The Object can be a regular Object, an Array, a Function, a RegExp, or any of the primitive types or constructor function created with new.

Optionally, you can set a callback function to be invoked before the object is garbage collected.

Object weak.get(Weakref ref)

get() returns the actual reference to the Object that this weak reference was created with. If this is called with a dead reference, undefined is returned.

Boolean weak.isDead(Weakref ref)

Checks to see if ref is a dead reference. Returns true if the original Object has already been GC'd, false otherwise.

Boolean weak.isWeakRef(Object obj)

Checks to see if obj is "weak reference" instance. Returns true if the passed in object is a "weak reference", false otherwise.

EventEmitter weak.addCallback(Weakref ref, Function callback)

Adds callback to the Array of callback functions that will be invoked before the Object gets garbage collected. The callbacks get executed in the order that they are added.

EventEmitter weak.removeCallback(Weakref ref, Function callback)

Removes callback from the Array of callback functions that will be invoked before the Object gets garbage collected.

EventEmitter weak.removeCallbacks(Weakref ref)

Empties the Array of callback functions that will be invoked before the Object gets garbage collected.

Array weak.callbacks(Weakref ref)

Returns an Array that ref iterates through to invoke the GC callbacks. This utilizes node's EventEmitter#listeners() function and therefore returns a copy in node 0.10 and newer.