Skip to content

Commit

Permalink
fix: silence tsup exports warning (#114)
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit authored Oct 13, 2024
1 parent 8d4b707 commit b1cbf04
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 99 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"node": true,
"commonjs": true
},
"ignorePatterns": ["tsup.config.ts"],
"rules": {
"import/no-extraneous-dependencies": "off",
"no-restricted-syntax": "off",
Expand Down
73 changes: 33 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const bench = new Bench({ time: 100 });

bench
.add('faster task', () => {
console.log('I am faster')
console.log('I am faster');
})
.add('slower task', async () => {
await new Promise(r => setTimeout(r, 1)) // we wait 1ms :)
console.log('I am slower')
await new Promise((r) => setTimeout(r, 1)); // we wait 1ms :)
console.log('I am slower');
})
.todo('unimplemented bench')
.todo('unimplemented bench');

await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
await bench.run();
Expand All @@ -60,9 +60,7 @@ console.table(bench.table());
// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │
// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘

console.table(
bench.table((task) => ({'Task name': task.name}))
);
console.table(bench.table((task) => ({ 'Task name': task.name })));

// Output:
// ┌─────────┬───────────────────────┐
Expand Down Expand Up @@ -139,13 +137,13 @@ export type Options = {
teardown?: Hook;
};

export type Hook = (task: Task, mode: "warmup" | "run") => void | Promise<void>;
export type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
```

- `async run()`: run the added tasks that were registered using the `add` method
- `async runConcurrently(threshold: number = Infinity, mode: "bench" | "task" = "bench")`: similar to the `run` method but runs concurrently rather than sequentially. See the [Concurrency](#Concurrency) section.
- `async warmup()`: warm up the benchmark tasks
- `async warmupConcurrently(threshold: number = Infinity, mode: "bench" | "task" = "bench")`: warm up the benchmark tasks concurrently
- `async runConcurrently(threshold: number = Infinity, mode: "bench" | "task" = "bench")`: similar to the `run` method but runs concurrently rather than sequentially. See the [Concurrency](#Concurrency) section.
- `async warmup()`: warmup the benchmark tasks
- `async warmupConcurrently(threshold: number = Infinity, mode: "bench" | "task" = "bench")`: warmup the benchmark tasks concurrently
- `reset()`: reset each task and remove its result
- `add(name: string, fn: Fn, opts?: FnOpts)`: add a benchmark task to the task map
- `Fn`: `() => any | Promise<any>`
Expand Down Expand Up @@ -176,7 +174,7 @@ function has been executed.
- `runs: number`: the number of times the task function has been executed
- `result?: TaskResult`: the result object
- `async run()`: run the current task and write the results in `Task.result` object
- `async warmup()`: warm up the current task
- `async warmup()`: warmup the current task
- `setResult(result: Partial<TaskResult>)`: change the result object values
- `reset()`: reset the task to make the `Task.runs` a zero-value and remove the `Task.result` object

Expand Down Expand Up @@ -210,7 +208,6 @@ the benchmark task result object.

```ts
export type TaskResult = {

/*
* the last error that was thrown while running the task
*/
Expand Down Expand Up @@ -318,40 +315,33 @@ in each class instance using the universal `addEventListener` and
* Bench events
*/
export type BenchEvents =
| "abort" // when a signal aborts
| "complete" // when running a benchmark finishes
| "error" // when the benchmark task throws
| "reset" // when the reset function gets called
| "start" // when running the benchmarks gets started
| "warmup" // when the benchmarks start getting warmed up (before start)
| "cycle" // when running each benchmark task gets done (cycle)
| "add" // when a Task gets added to the Bench
| "remove" // when a Task gets removed of the Bench
| "todo"; // when a todo Task gets added to the Bench
| 'abort' // when a signal aborts
| 'complete' // when running a benchmark finishes
| 'error' // when the benchmark task throws
| 'reset' // when the reset function gets called
| 'start' // when running the benchmarks gets started
| 'warmup' // when the benchmarks start getting warmed up (before start)
| 'cycle' // when running each benchmark task gets done (cycle)
| 'add' // when a Task gets added to the Bench
| 'remove' // when a Task gets removed of the Bench
| 'todo'; // when a todo Task gets added to the Bench

/**
* task events
*/
export type TaskEvents =
| "abort"
| "complete"
| "error"
| "reset"
| "start"
| "warmup"
| "cycle";
export type TaskEvents = 'abort' | 'complete' | 'error' | 'reset' | 'start' | 'warmup' | 'cycle';
```

For instance:

```js
// runs on each benchmark task's cycle
bench.addEventListener("cycle", (e) => {
bench.addEventListener('cycle', (e) => {
const task = e.task!;
});

// runs only on this benchmark task's cycle
task.addEventListener("cycle", (e) => {
task.addEventListener('cycle', (e) => {
const task = e.task!;
});
```
Expand All @@ -365,12 +355,14 @@ export type BenchEvent = Event & {
```

### `process.hrtime`

if you want more accurate results for nodejs with `process.hrtime`, then import
the `hrtimeNow` function from the library and pass it to the `Bench` options.
the `hrtimeNow` function from the library and pass it to the `Bench` options.

```ts
import { hrtimeNow } from 'tinybench';
```

It may make your benchmarks slower, check #42.

## Concurrency
Expand All @@ -381,20 +373,21 @@ It may make your benchmarks slower, check #42.

```ts
// options way (recommended)
bench.threshold = 10 // The maximum number of concurrent tasks to run. Defaults to Infinity.
bench.concurrency = "task" // The concurrency mode to determine how tasks are run.
// await bench.warmup()
await bench.run()
bench.threshold = 10; // The maximum number of concurrent tasks to run. Defaults to Infinity.
bench.concurrency = 'task'; // The concurrency mode to determine how tasks are run.
// await bench.warmup();
await bench.run();

// standalone method way
// await bench.warmupConcurrently(10, "task")
await bench.runConcurrently(10, "task") // with runConcurrently, mode is set to 'bench' by default
// await bench.warmupConcurrently(10, 'task');
await bench.runConcurrently(10, 'task'); // with runConcurrently, mode is set to 'bench' by default
```

## Prior art

- [Benchmark.js](https://github.com/bestiejs/benchmark.js)
- [Mitata](https://github.com/evanwashere/mitata/)
- [tatami-ng](https://github.com/poolifier/tatami-ng)
- [Bema](https://github.com/prisma-labs/bema)

## Authors
Expand Down
9 changes: 7 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
"name": "examples",
"version": "0.0.0",
"type": "module",
"packageManager": "[email protected]",
"volta": {
"node": "20.18.0",
"pnpm": "8.15.9"
},
"scripts": {
"build": "tsc",
"all": "pnpm run simple",
"all": "pnpm simple",
"simple": "tsx src/simple.ts",
"dev:simple": "tsx watch src/simple.ts"
"dev:simple": "tsx --watch src/simple.ts"
},
"license": "ISC",
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions examples/src/simple.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { Bench } from '../../src';

const bench = new Bench({ time: 100 });
Expand Down
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@
"dev": "tsup --watch",
"build": "tsup",
"prepare": "git config core.hooksPath .hooks",
"publish": "npm run build && clean-publish",
"publish": "pnpm build && clean-publish",
"typecheck": "tsc --noEmit",
"lint": "eslint src test examples",
"lint:fix": "eslint --fix src test examples",
"release": "bumpp package.json --commit --push --tag && npm run publish",
"lint": "eslint src test examples tsup.config.ts",
"lint:fix": "eslint --fix src test examples tsup.config.ts",
"release": "bumpp package.json --commit --push --tag && pnpm publish",
"test": "vitest --retry=5 --run"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.cts",
"types": "./dist/index.d.ts",
"exports": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"default": "./dist/index.js"
"require": {
"types": "./dist/index.d.cts",
"require": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"files": [
"dist/**"
Expand All @@ -39,7 +44,7 @@
"@typescript-eslint/parser": "^7.18.0",
"bumpp": "^9.7.1",
"changelogithub": "^0.13.11",
"clean-publish": "^3.4.5",
"clean-publish": "^5.0.0",
"eslint": "^8.57.1",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.31.0",
Expand Down
48 changes: 7 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
"skipLibCheck": true,
"lib": []
},
"include": ["src", "test", "@types"],
"exclude": ["node_modules", "dist"],
"include": [
"src",
"test",
"tsup.config.ts"
],
"exclude": [
"node_modules",
"dist"
],
"removeComments": true,
"newLine": "lf"
}
8 changes: 4 additions & 4 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineConfig } from "tsup";
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
format: ["esm", "cjs"],
entry: ['src/index.ts'],
outDir: 'dist',
format: ['esm', 'cjs'],
minify: false,
minifySyntax: true,
minifyWhitespace: false,
Expand Down

0 comments on commit b1cbf04

Please sign in to comment.