nano-benchmark 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,8 +36,24 @@ npm install --save nano-benchmark
36
36
 
37
37
  Both [deno](https://deno.land/) and [bun](https://bun.sh/) are supported.
38
38
 
39
+ If you want to run the benchmark in Deno, Bun, etc. you can specify `self` as the `file` argument
40
+ or the `--self` option.
41
+ In this case the utility will print out its file name to `stdout` and exit. It allows running
42
+ the utility with alternative JavaScript interpreters.
43
+
44
+ Examples with `bash`:
45
+
46
+ ```bash
47
+ $ npx nano-bench benchmark.js
48
+ $ bun `npx nano-bench --self` benchmark.js
49
+ $ deno run --allow-read --allow-hrtime `npx nano-bench --self` benchmark.js
50
+ $ deno run -A `npx nano-bench --self` benchmark.js
51
+ $ node `npx nano-bench --self` benchmark.js
52
+ ```
53
+
39
54
  Don't forget to specify the appropriate permissions for Deno to run the benchmark scripts:
40
- `--allow-read` (required) and `--allow-hrtime` (optional but recommended).
55
+ `--allow-read` (required) and `--allow-hrtime` (optional but recommended). Or consider using
56
+ `-A` or `--allow-all` to allow all permissions (used it only in safe environments!).
41
57
 
42
58
  ## Documentation
43
59
 
@@ -96,4 +112,6 @@ BSD 3-Clause License
96
112
 
97
113
  ## Release history
98
114
 
115
+ - 1.0.2: *Added the `--self` option.*
116
+ - 1.0.1: *Added "self" argument to utilities so it can be used with Deno, Bun, etc.*
99
117
  - 1.0.0: *Initial release.*
package/bin/nano-bench.js CHANGED
@@ -1,6 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import process from 'node:process';
4
+ import {fileURLToPath, pathToFileURL} from 'node:url';
5
+ import path from 'node:path';
6
+ import {readFile} from 'node:fs/promises';
4
7
 
5
8
  import {Option, program} from 'commander';
6
9
 
@@ -29,11 +32,24 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
29
32
  toInt = value => parseInt(value),
30
33
  toFloat = value => parseFloat(value);
31
34
 
35
+ const filePath = new URL('../package.json', import.meta.url),
36
+ pkg = JSON.parse(await readFile(filePath, {encoding: 'utf8'}));
37
+
38
+ const showSelf = () => {
39
+ const self = new URL(import.meta.url);
40
+ if (self.protocol === 'file:') {
41
+ console.log(fileURLToPath(self));
42
+ } else {
43
+ console.log(self);
44
+ }
45
+ process.exit(0);
46
+ };
47
+
32
48
  program
33
49
  .name('nano-bench')
50
+ .version(pkg.version)
34
51
  .description('Small utility to benchmark and compare code.')
35
- .version('1.0.0')
36
- .argument('<file>', 'File to benchmark')
52
+ .argument('<file>', 'File to benchmark.\nIf "self", returns its file name to stdout and exits')
37
53
  .option('-m, --ms <ms>', 'measurement time in milliseconds', toInt, 50)
38
54
  .addOption(
39
55
  new Option('-i, --iterations <iterations>', 'measurement iterations (overrides --ms)')
@@ -46,13 +62,18 @@ program
46
62
  .option('-s, --samples <samples>', 'number of samples', toInt, 100)
47
63
  .option('-p, --parallel', 'take samples in parallel asynchronously')
48
64
  .option('-b, --bootstrap <bootstrap>', 'number of bootstrap samples', toInt, 1000)
65
+ .option('--self', 'returns the file name to stdout and exits')
49
66
  .showHelpAfterError('(add --help to see available options)');
50
67
 
68
+ program.on('option:self', showSelf);
69
+
51
70
  program.parse();
52
71
 
53
72
  const options = program.opts(),
54
73
  args = program.args;
55
74
 
75
+ if (args[0] === 'self') showSelf();
76
+
56
77
  // validate the options
57
78
 
58
79
  if (options.minIterations < 1) program.error('The minimum number of iterations must be >= 1');
@@ -63,7 +84,7 @@ if (options.bootstrap < 1) program.error('The number of bootstrap samples must b
63
84
 
64
85
  // open the file
65
86
 
66
- const fileName = new URL(args[0], `file://${process.cwd()}/`);
87
+ const fileName = pathToFileURL(path.join(process.cwd(), args[0]));
67
88
 
68
89
  let fns;
69
90
  try {
package/bin/nano-watch.js CHANGED
@@ -1,6 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import process from 'node:process';
4
+ import {fileURLToPath, pathToFileURL} from 'node:url';
5
+ import path from 'node:path';
6
+ import {readFile} from 'node:fs/promises';
4
7
 
5
8
  import {program} from 'commander';
6
9
 
@@ -24,25 +27,43 @@ import {StatCounter} from '../src/stream-stats.js';
24
27
 
25
28
  const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
26
29
 
30
+ const filePath = new URL('../package.json', import.meta.url),
31
+ pkg = JSON.parse(await readFile(filePath, {encoding: 'utf8'}));
32
+
33
+ const showSelf = () => {
34
+ const self = new URL(import.meta.url);
35
+ if (self.protocol === 'file:') {
36
+ console.log(fileURLToPath(self));
37
+ } else {
38
+ console.log(self);
39
+ }
40
+ process.exit(0);
41
+ };
42
+
27
43
  program
28
44
  .name('nano-watch')
29
45
  .description('Small utility to continuously benchmark code.')
30
- .version('1.0.0')
31
- .argument('<file>', 'File to benchmark')
46
+ .version(pkg.version)
47
+ .argument('<file>', 'File to benchmark.\nIf "self", returns its file name to stdout and exits')
32
48
  .argument('[method]', 'Method name to benchmark')
33
49
  .option('-m, --ms <ms>', 'milliseconds per iteration', value => parseInt(value), 500)
34
50
  .option('-i, --iterations <number>', 'number of iterations (default: Infinity)', value =>
35
51
  parseInt(value)
36
52
  )
37
53
  .option('-e, --export <name>', 'name of the export in the file', 'default')
54
+ .option('--self', 'print the file name to stdout and exit')
38
55
  .showHelpAfterError('(add --help to see available options)');
39
56
 
57
+ program.on('option:self', showSelf);
58
+
40
59
  program.parse();
41
60
 
42
61
  const options = program.opts(),
43
62
  args = program.args;
44
63
 
45
- const fileName = new URL(args[0], `file://${process.cwd()}/`);
64
+ if (args[0] === 'self') showSelf();
65
+
66
+ const fileName = pathToFileURL(path.join(process.cwd(), args[0]));
46
67
 
47
68
  let fn;
48
69
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nano-benchmark",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Small utilities to benchmark code with Node.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",