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

bgbash

v1.0.0

Published

Running a background bash to reduce the amount of child processes.

Downloads

4

Readme

bgbash

Build Status JavaScript Style Guide Maintainability Test Coverage

bgbash is a partial drop-in replacement for require('child_process').exec specifically made for long-running applications.

npm i bgbash --save

Why?

Starting a child process using spawn or exec will create a new instance of ChildProcess, open several streams and create a new system background process. This is very expensive if you do it often. bgbash starts a single background process on the the first request, every future request will use that process's stdin to execute commands. Reducing the startup and feedback time for subsequent calls.

Performance comparison

| "echo hi" - 2000 runs on node-v10.12.0(darwin) | node.js | bgback | notes | |-----------------|---------|--------|---| | startup | 10.98ms | 16.05ms | 46% slower - The startup is naturally slower as it does a little more. | | repeat response | 4.91ms | 1.96ms | 251% faster - But repeat calls are significantly faster, | | repeat user | 1.31ms | 0.95ms | 137% faster - with part of it coming from the reduced user execution time ... | | repeat system | 0.64ms | 0.04ms | 1457% faster - ... and a significantly reduced system execution time. | | rss | 37.9Mb (avg. 19.8Mb) | 9.9Mb (avg. 6.6Mb) | With a significantly lower rss memory allocation (which is stable even with more calls) | | heap total | 37.5Mb (avg. 21.4Mb) | 6Mb (avg. 3.2Mb) | The node.js version also fills up the heap a lot quicker to a avg. 32Mb use at 10000 execs while the bgback version needs around 20000 to get there. | | heap used | 19.5Mb (avg. 6.3Mb) | 4.1Mb (avg. 1.4Mb) | The difference in size can be attributed to the additional code loaded. This will slightly grow with number of calls (~20kb per 5000 calls). Reason is unclear but consistent for both the node.js and bgback version. | | c-memory inc. | 8.6Kb (avg. -201b) | 170b (avg. -7.8Kb) | The C++ memory can be negative as some initial c memory is cleared. |

Note: This data is compiled using the ./perf.js script.

API compatibility

The API of exec is implemented to be a reasonable (but not feature complete) drop-in replacement for node's native exec.

const { exec } = require('bgbash')

const cmd = 'echo hi'
const opts = { // (optional)
  cwd: '.',             // (optional) Path in which the code is to be executed, defaults to `process.cwd()`.
  env: { KEY: 'value' } // (optional) Environment variables to be used for the execution.
  timeout: 100,         // (optional) Time in milliseconds until a timeout appear, defaults to `0` = no timeout.
  encoding: 'utf8',     // (optional) Encoding to be used, an unknown or `null` encoding returns a Buffer.
}

exec(cmd, opts, (error, stdout, stderr) => {
  error // Error that might occur, i.e. TIMEOUT or of the process died.
  stdout // Output of the execution
  stderr // Error of the execution
})

A notable incompatibility is that stderr will be empty, even though output might exist, if no error occurs. This is done for performance reasons.

Closing at shutdown

The background process will continue to run forever. If you want the process to close, you have to run close().

const { exec, close } = require('bgbash')

exec('echo hi')
close( // Will be executed after the previous command completed!
  () => {
    // All closed!
  }
)

Promises API

Much like node js, bgbash also comes with a Promise API that is available using either require('bgbash').promises or require('bgbash/promises').

const { exec } = require('bgbash').promises
const opts = { /* Same options as above. */ }
const { stdout, stderr } = await exec('echo hi', opts)

try {
  await exec('exit 1', opts)
} catch (error) {
  // Error that happened.
}

License

MIT