Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> **Part of [Tuulbelt](https://github.com/tuulbelt/tuulbelt)** — A collection of zero-dependency tools.

# Structured Error Handler / `serr`

[![Tests](https://github.com/tuulbelt/structured-error-handler/actions/workflows/test.yml/badge.svg)](https://github.com/tuulbelt/structured-error-handler/actions/workflows/test.yml)
Expand Down
93 changes: 93 additions & 0 deletions benchmarks/index.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env node --import tsx
/**
* Structured Error Handler Benchmarks
*
* Measures performance of core operations using tatami-ng for statistical rigor.
*
* Run: npm run bench
*
* See: /docs/BENCHMARKING_STANDARDS.md
*/

import { bench, baseline, group, run } from 'tatami-ng';
import { StructuredError, ErrorContext, toJSON, fromJSON } from '../src/index.ts';

// Prevent dead code elimination
let result: StructuredError | string | ErrorContext;

// Sample data for benchmarking
const simpleContext: ErrorContext = { operation: 'test' };
const richContext: ErrorContext = {
operation: 'database.query',
userId: 'user-123',
requestId: 'req-456',
timestamp: Date.now(),
metadata: { table: 'users', query: 'SELECT * FROM users WHERE id = ?' },
};

// ============================================================================
// Core Operations Benchmarks
// ============================================================================

group('Error Creation', () => {
baseline('create: simple error', () => {
result = new StructuredError('Something went wrong', 'GENERIC_ERROR');
});

bench('create: with simple context', () => {
result = new StructuredError('Operation failed', 'OP_FAILED', simpleContext);
});

bench('create: with rich context', () => {
result = new StructuredError('Database query failed', 'DB_ERROR', richContext);
});

bench('create: with cause chain', () => {
const cause = new Error('Connection timeout');
result = new StructuredError('Database error', 'DB_ERROR', richContext, cause);
});
});

group('Error Wrapping', () => {
const nativeError = new Error('Native error message');

baseline('wrap: native Error', () => {
result = StructuredError.wrap(nativeError);
});

bench('wrap: with context', () => {
result = StructuredError.wrap(nativeError, richContext);
});

bench('wrap: with code override', () => {
result = StructuredError.wrap(nativeError, richContext, 'WRAPPED_ERROR');
});
});

group('Serialization', () => {
const error = new StructuredError('Test error', 'TEST', richContext);

baseline('toJSON: serialize', () => {
result = toJSON(error);
});

bench('fromJSON: deserialize', () => {
const json = toJSON(error);
result = fromJSON(json);
});

bench('round-trip: serialize + deserialize', () => {
const json = toJSON(error);
result = fromJSON(json);
});
});

// ============================================================================
// Run Benchmarks
// ============================================================================

await run({
units: false,
silent: false,
json: false,
});
29 changes: 28 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build": "tsc",
"test": "node --import tsx --test test/index.test.ts",
"test:watch": "node --import tsx --test --watch test/index.test.ts",
"bench": "node --import tsx benchmarks/index.bench.ts",
"dogfood": "npm run dogfood:flaky && npm run dogfood:diff",
"dogfood:flaky": "flaky --test 'npm test' --runs 10",
"dogfood:diff": "npx tsx test/dogfood-diff.ts"
Expand All @@ -36,7 +37,8 @@
},
"devDependencies": {
"@tuulbelt/test-flakiness-detector": "git+https://github.com/tuulbelt/test-flakiness-detector.git",
"@types/node": "^20.0.0",
"@types/node": "^20.19.27",
"tatami-ng": "^0.8.0",
"tsx": "^4.7.0",
"typescript": "^5.3.0"
}
Expand Down