nano-benchmark 1.0.1 → 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 +7 -5
- package/bin/nano-bench.js +23 -8
- package/bin/nano-watch.js +23 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,7 +36,8 @@ 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
|
|
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.
|
|
40
41
|
In this case the utility will print out its file name to `stdout` and exit. It allows running
|
|
41
42
|
the utility with alternative JavaScript interpreters.
|
|
42
43
|
|
|
@@ -44,10 +45,10 @@ Examples with `bash`:
|
|
|
44
45
|
|
|
45
46
|
```bash
|
|
46
47
|
$ npx nano-bench benchmark.js
|
|
47
|
-
$ bun `npx nano-bench self` benchmark.js
|
|
48
|
-
$ deno run --allow-read --allow-hrtime `npx nano-bench self` benchmark.js
|
|
49
|
-
$ deno run -A `npx nano-bench self` benchmark.js
|
|
50
|
-
$ node `npx nano-bench self` 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
|
|
51
52
|
```
|
|
52
53
|
|
|
53
54
|
Don't forget to specify the appropriate permissions for Deno to run the benchmark scripts:
|
|
@@ -111,5 +112,6 @@ BSD 3-Clause License
|
|
|
111
112
|
|
|
112
113
|
## Release history
|
|
113
114
|
|
|
115
|
+
- 1.0.2: *Added the `--self` option.*
|
|
114
116
|
- 1.0.1: *Added "self" argument to utilities so it can be used with Deno, Bun, etc.*
|
|
115
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
|
-
.
|
|
36
|
-
.argument('<file>', 'File to benchmark.\nIf "self", returns its file name to stdout and exits.')
|
|
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,18 +62,17 @@ 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
|
|
|
56
|
-
if (args[0] === 'self')
|
|
57
|
-
const name = String(import.meta.url);
|
|
58
|
-
console.log(name.startsWith('file://') ? name.slice(7) : name);
|
|
59
|
-
process.exit(0);
|
|
60
|
-
}
|
|
75
|
+
if (args[0] === 'self') showSelf();
|
|
61
76
|
|
|
62
77
|
// validate the options
|
|
63
78
|
|
|
@@ -69,7 +84,7 @@ if (options.bootstrap < 1) program.error('The number of bootstrap samples must b
|
|
|
69
84
|
|
|
70
85
|
// open the file
|
|
71
86
|
|
|
72
|
-
const fileName =
|
|
87
|
+
const fileName = pathToFileURL(path.join(process.cwd(), args[0]));
|
|
73
88
|
|
|
74
89
|
let fns;
|
|
75
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,31 +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(
|
|
31
|
-
.argument('<file>', 'File to benchmark.\nIf "self", returns its file name to stdout and exits
|
|
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
|
-
if (args[0] === 'self')
|
|
46
|
-
const name = String(import.meta.url);
|
|
47
|
-
console.log(name.startsWith('file://') ? name.slice(7) : name);
|
|
48
|
-
process.exit(0);
|
|
49
|
-
}
|
|
64
|
+
if (args[0] === 'self') showSelf();
|
|
50
65
|
|
|
51
|
-
const fileName =
|
|
66
|
+
const fileName = pathToFileURL(path.join(process.cwd(), args[0]));
|
|
52
67
|
|
|
53
68
|
let fn;
|
|
54
69
|
try {
|