|
| 1 | +/** |
| 2 | + * Benchmark for overhead of computationally expensive functions. |
| 3 | + * |
| 4 | + * Currently the effected version is around 100x (`fibPipe`) to 200x (`fibGen`) slower than the |
| 5 | + * non-effected version. |
| 6 | + * |
| 7 | + * Such overhead should be acceptable for most use-cases, as real-world applications are unlikely to |
| 8 | + * execute computationally expensive logic in an effected function. |
| 9 | + * |
| 10 | + * It is worth noting that tinyeffect’s version using generator syntax is around 20% faster than |
| 11 | + * Effect’s version using `Effect.gen`, but the one using pipeline syntax is around 30%~120% slower |
| 12 | + * than Effect’s version, which is quite surprising. A further investigation is needed to find out |
| 13 | + * why Effect’s version is faster than tinyeffect’s version using pipeline syntax. |
| 14 | + */ |
| 15 | + |
| 16 | +import { Effect } from "effect"; |
| 17 | +import { bench, describe } from "vitest"; |
| 18 | + |
| 19 | +import { Effected, effected } from "."; |
| 20 | + |
| 21 | +const fib = (n: number): number => { |
| 22 | + if (n <= 1) return n; |
| 23 | + return fib(n - 1) + fib(n - 2); |
| 24 | +}; |
| 25 | + |
| 26 | +const fibGen = (n: number): Effected<never, number> => |
| 27 | + effected(function* () { |
| 28 | + if (n <= 1) return n; |
| 29 | + return (yield* fibGen(n - 1)) + (yield* fibGen(n - 2)); |
| 30 | + }); |
| 31 | + |
| 32 | +const fibPipe = (n: number): Effected<never, number> => { |
| 33 | + if (n <= 1) return Effected.of(n); |
| 34 | + return fibPipe(n - 1).map((a) => fibPipe(n - 2).map((b) => a + b)); |
| 35 | +}; |
| 36 | + |
| 37 | +const fibEGen = (n: number): Effect.Effect<number, never, never> => |
| 38 | + Effect.gen(function* () { |
| 39 | + if (n <= 1) return n; |
| 40 | + return (yield* fibEGen(n - 1)) + (yield* fibEGen(n - 2)); |
| 41 | + }); |
| 42 | + |
| 43 | +const fibEPipe = (n: number): Effect.Effect<number, never, never> => { |
| 44 | + if (n <= 1) return Effect.succeed(n); |
| 45 | + return fibEPipe(n - 1).pipe( |
| 46 | + Effect.flatMap((a) => fibEPipe(n - 2).pipe(Effect.map((b) => a + b))), |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +describe("fib(20)", () => { |
| 51 | + bench("fib(20)", () => void fib(20)); |
| 52 | + bench("fibGen(20)", () => void fibGen(20).runSync()); |
| 53 | + bench("fibPipe(20)", () => void fibPipe(20).runSync()); |
| 54 | + bench("fibEGen(20)", () => void Effect.runSync(fibEGen(20))); |
| 55 | + bench("fibEPipe(20)", () => void Effect.runSync(fibEPipe(20))); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("fib(30)", () => { |
| 59 | + bench("fib(30)", () => void fib(30)); |
| 60 | + bench("fibGen(30)", () => void fibGen(30).runSync()); |
| 61 | + bench("fibPipe(30)", () => void fibPipe(30).runSync()); |
| 62 | + bench("fibEGen(30)", () => void Effect.runSync(fibEGen(30))); |
| 63 | + bench("fibEPipe(30)", () => void Effect.runSync(fibEPipe(30))); |
| 64 | +}); |
0 commit comments