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

ddebuglog

v0.2.1

Published

Dynamic Debuglog

Downloads

4

Readme

Dynamic Debuglog

Let's say you're using util.debuglog to write debugging statements (like you should), and you wish you could hook in to process.env.NODE_DEBUG to dynamically turn them on and off without restarting your process. This allows you to do that.

Maybe you have a repl available in your app, or you open a route, or maybe you want to trigger it programmatically. If you can set a variable on the process.env object, you can turn your debugging statements on and off.

Performance Notes

In order to be dynamic, this debugger must check process.env.NODE_DEBUG on each debug statement. When in debug mode, this additional check is totally negligible. But when not debugging, it still must perform the check. This is much slower than the empty function that core util.debuglog calls when not in debug mode.

No problem! When you instantiate a dynamic debug logger, it will make a one-time check at process.env.NODE_ENV, so it can be parameterized to revert to the static core version of util.debuglog in certain environments. You can be dynamic in development and you can sacrifice flexibility for speed in production - with no change in code. (See stingy mode below for how this works.)

See below for benchmarks. You may choose to go dynamic all the way.

API: Comparison with util.debuglog

ddbuglog is designed to be used exactly like you use util.debuglog.

var coreDebug = require('util').debuglog('neatstuff');
var dynamicDebug = require('ddebuglog')('neatstuff');

coreDebug('This statement never prints.');
dynamicDebug('This statement never prints.');

process.env.NODE_DEBUG = 'neatstuff';

coreDebug('This statement still never prints');
dynamicDebug('But this one prints, because NODE_DEBUG is now %s', process.env.NODE_DEBUG);

// prints to stderr:
// NEATSTUFF 4918: But this one prints, because NODE_DEBUG is now neatstuff

Stingy mode

Can't afford the milliseconds? Youf can choose to revert to core util.debug in certain environments. Pass in an object with a useStatic attribute for the second argument:

// process.env.NODE_DEBUG not set to 'neatstuff'
var flexibleDebugger = dynamicDebug('neatstuff', {useStatic: 'production'});

process.env.NODE_DEBUG = 'neatstuff';

debug('This will still not print if process.env.NODE_ENV is `production`');

useStatic can be an Array of strings or a single string. If present when creating a ddebuglog, it will revert to the static core module when process.env.NODE_ENV matches. Simple enough, right?

Benchmarks

Using benchmark.js on a MacBook Air 2.2GHz i7 with 8GB RAM, here is a comparison of Node core util.debuglog and ddebuglog. As you can see, running in stingy mode is exactly the same as core (because that's all it is.

If you want to stay dynamic all the way, you can pass over 1500 non-active debug statements and stilladd only 1ms of latency.

[Baseline] Running an empty function x 96,610,538 ops/sec ±0.97% (94 runs sampled)
[Non-debug mode] Dynamic debuglog x 1,499,211 ops/sec ±0.99% (93 runs sampled)
[Non-debug mode] Node core debuglog x 97,839,585 ops/sec ±0.76% (94 runs sampled)
[Non-debug mode] Dynamic debuglog with active useStatic x 96,241,918 ops/sec ±1.91% (92 runs sampled)

[Debug mode] Dynamic debuglog x 160,839 ops/sec ±1.33% (91 runs sampled)
[Debug mode] Node core debuglog x 165,748 ops/sec ±1.28% (92 runs sampled)

Test

npm test to see it in action.

License

MIT

Hopes

You find it useful. :)