initial commit

This commit is contained in:
2026-03-30 08:34:35 +02:00
commit 49d8ea67ba
103 changed files with 22387 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
import { bench, describe } from 'vitest';
import { STANDARD } from '../helpers/bench-options.js';
import {
okSome,
okNone,
errNone,
E,
O,
pipe,
} from '../helpers/imports.js';
const double = (x: number) => x * 2;
const toUpper = (e: string) => e.toUpperCase();
// ---------------------------------------------------------------------------
// map — OkSome path
// ---------------------------------------------------------------------------
describe('Query.map — OkSome path', () => {
const nm = okSome(42);
const fp = E.right(O.some(42));
bench('native-monad', () => { nm.map(double); }, STANDARD);
bench('fp-ts (nested)', () => {
pipe(fp, E.map(O.map(double)));
}, STANDARD);
});
// ---------------------------------------------------------------------------
// map — OkNone path (no-op)
// ---------------------------------------------------------------------------
describe('Query.map — OkNone path (no-op)', () => {
const nm = okNone();
const fp = E.right(O.none);
bench('native-monad', () => { nm.map(double); }, STANDARD);
bench('fp-ts (nested)', () => {
pipe(fp, E.map(O.map(double)));
}, STANDARD);
});
// ---------------------------------------------------------------------------
// map — ErrNone path (no-op)
// ---------------------------------------------------------------------------
describe('Query.map — ErrNone path (no-op)', () => {
const nm = errNone('fail');
const fp = E.left('fail');
bench('native-monad', () => { nm.map(double); }, STANDARD);
bench('fp-ts (nested)', () => {
pipe(fp, E.map(O.map(double)));
}, STANDARD);
});
// ---------------------------------------------------------------------------
// flatMap — OkSome path
// ---------------------------------------------------------------------------
describe('Query.flatMap — OkSome path', () => {
const nm = okSome(42);
const fp = E.right(O.some(42));
bench('native-monad', () => { nm.flatMap(x => okSome(x * 2)); }, STANDARD);
bench('fp-ts (nested)', () => {
pipe(
fp,
E.flatMap(inner =>
pipe(
inner,
O.match(
() => E.right(O.none) as E.Either<string, O.Option<number>>,
v => E.right(O.some(v * 2)),
),
),
),
);
}, STANDARD);
});
// ---------------------------------------------------------------------------
// mapErr — ErrNone path
// ---------------------------------------------------------------------------
describe('Query.mapErr — ErrNone path', () => {
const nm = errNone('fail');
const fp = E.left('fail');
bench('native-monad', () => { nm.mapErr(toUpper); }, STANDARD);
bench('fp-ts (nested)', () => {
pipe(fp, E.mapLeft(toUpper));
}, STANDARD);
});
// ---------------------------------------------------------------------------
// match — three-way
// ---------------------------------------------------------------------------
describe('Query.match — OkSome (three-way dispatch)', () => {
const nm = okSome(42);
const fp = E.right(O.some(42));
bench('native-monad', () => {
nm.match(v => v * 2, () => 0, () => -1);
}, STANDARD);
bench('fp-ts (nested, two match calls)', () => {
pipe(
fp,
E.match(
() => -1,
O.match(() => 0, v => v * 2),
),
);
}, STANDARD);
});
describe('Query.match — OkNone (three-way dispatch)', () => {
const nm = okNone();
const fp = E.right(O.none);
bench('native-monad', () => {
nm.match(() => 1, () => 0, () => -1);
}, STANDARD);
bench('fp-ts (nested)', () => {
pipe(
fp,
E.match(
() => -1,
O.match(() => 0, () => 1),
),
);
}, STANDARD);
});
describe('Query.match — ErrNone (three-way dispatch)', () => {
const nm = errNone('fail');
const fp = E.left('fail');
bench('native-monad', () => {
nm.match(() => 1, () => 0, e => e.length);
}, STANDARD);
bench('fp-ts (nested)', () => {
pipe(
fp,
E.match(
e => e.length,
O.match(() => 0, () => 1),
),
);
}, STANDARD);
});
// ---------------------------------------------------------------------------
// filter — OkSome path
// ---------------------------------------------------------------------------
describe('Query.filter — predicate passes', () => {
const nm = okSome(42);
bench('native-monad', () => { nm.filter(x => x > 0); }, STANDARD);
// No other library has a three-state filter
});
describe('Query.filter — predicate fails', () => {
const nm = okSome(-1);
bench('native-monad', () => { nm.filter(x => x > 0); }, STANDARD);
});