Skip to content

Repository files navigation

πŸ”­ NodeSand

NodeSand is a powerful CLI utility tool that provides various tools for experimenting with simple code or benchmarking in a Node.js environment.

Tue Jan 31 2023 09:03:30 GMT+0900 (Korean Standard Time)
==================================================
β ‹ Main function is running...
Hello, World!
βœ” Done!
==================================================
50.02708297967911 ms

Features

  • Serve static HTML file
  • Code benchmarking
  • TypeScript support
  • ESLint & Prettier support
  • Observation mode
  • Execution time report

Installation

We recommend using Node.js version 20 or higher to use NodeSand.

$ npm init nodesand

Usage

Before you begin, you need to install the dependent packages.

$ npm i
# or...
$ pnpm i

Run node script

Source files of node scripts (dev, once, watch) are located in src/node/index.js. When all the actions are done, we call next() to end execution.

Run the script at once with the following command:

$ npm run once
or
$ npm run dev

Run whenever the script file changes with the following command:

$ npm run watch

Benchmarking

You can run benchmarks using various code comparison sets in Node.js. (We use Tinybench for benchmarking.)

Write the code in src/bench/index.js as follows:

export const benchConfiguration = { name: 'Benchmark', time: 100 };

export default function defineBenchmarks(bench) {
  bench
    .add('faster task', () => {
      console.log('I am faster');
    })
    .add('slower task', async () => {
      await new Promise((resolve) => setTimeout(resolve, 1)); // we wait 1ms :)
      console.log('I am slower');
    });
}

Then simply run the following command:

$ npm run bench

Then the benchmarking results will be displayed as shown in the example below.

βœ” Done!
===========================================
Benchmark:
791.840458 ms
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ Task name     β”‚ Latency avg (ns)  β”‚ Latency med (ns)   β”‚ Throughput avg (ops/s) β”‚ Throughput med (ops/s) β”‚ Samples β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 0       β”‚ 'faster task' β”‚ '3861.8 Β± 2.74%'  β”‚ '3500.0 Β± 166.00'  β”‚ '277861 Β± 0.12%'       β”‚ '285714 Β± 12937'       β”‚ 25895   β”‚
β”‚ 1       β”‚ 'slower task' β”‚ '1147313 Β± 3.46%' β”‚ '1174041 Β± 9520.5' β”‚ '1642 Β± 66.76%'        β”‚ '852 Β± 7'              β”‚ 88      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Static HTML serve

Changes in HTML files as well as node scripts can be displayed immediately in a web browser.

Source files of HTML files are located in src/www/index.html.

To serve a static HTML file, run the following command:

$ npm run serve
or
$ npm run www

Now you can try opening the HTML file with http://localhost:3000. Any changes will be updated immediately. (requires refresh)

Quickly restart the task

Use the command below to quickly reset the code to its initial state:

# Code reset
$ npm run reset
# Additionally, initialize `package.json` and reset the modules.
$ npm run reset:all

Contributing

Anyone can contribute to the project by reporting new issues or submitting a pull request. For more information, please see CONTRIBUTING.md.

License

Please see the LICENSE file for more information about project owners, usage rights, and more.

About

πŸ§ͺ Node.js Sandbox provides a template to run or test simple Node.js scripts.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

6 stars

Watchers

1 watching

Forks

Sponsor this project

Used by

Contributors

Languages