Skip to content

Commit

Permalink
Update bench
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed May 5, 2024
1 parent 8fb5d6e commit f86aea2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 21 deletions.
36 changes: 22 additions & 14 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,38 @@ Tested on a M1 Pro MacBook Pro.
## Option

```
fp-ts Option none x 133,199,621 ops/sec ±0.88% (95 runs sampled)
fp-ts Option some x 94,608,143 ops/sec ±1.02% (95 runs sampled)
fp-ts Option some chain x 2,704,227 ops/sec ±1.19% (96 runs sampled)
effect Option none x 27,023,109 ops/sec ±1.29% (88 runs sampled)
effect Option some x 24,754,366 ops/sec ±1.15% (93 runs sampled)
effect Option some chain x 23,508,577 ops/sec ±0.79% (92 runs sampled)
Boxed Option none x 1,021,669,460 ops/sec ±0.87% (100 runs sampled)
Boxed Option some x 455,095,023 ops/sec ±1.37% (97 runs sampled)
Boxed Option some flatMap x 461,052,470 ops/sec ±1.00% (96 runs sampled)
fp-ts Option none x 136,289,948 ops/sec ±0.35% (99 runs sampled)
fp-ts Option some x 92,748,146 ops/sec ±0.36% (95 runs sampled)
fp-ts Option some chain x 2,831,502 ops/sec ±0.21% (98 runs sampled)
effect Option none x 28,316,950 ops/sec ±0.68% (90 runs sampled)
effect Option some x 24,920,467 ops/sec ±0.78% (92 runs sampled)
effect Option some chain x 24,060,988 ops/sec ±0.31% (99 runs sampled)
Boxed Option none x 613,986,228 ops/sec ±0.20% (99 runs sampled)
Boxed Option some x 445,172,964 ops/sec ±0.50% (100 runs sampled)
Boxed Option some flatMap x 447,362,963 ops/sec ±0.53% (98 runs sampled)
```

## Result

```
fp-ts Result x 116,286,025 ops/sec ±0.81% (95 runs sampled)
effect Result x 25,582,523 ops/sec ±0.80% (99 runs sampled)
Boxed Result x 439,563,425 ops/sec ±1.00% (99 runs sampled)
fp-ts Result x 116,379,320 ops/sec ±0.48% (95 runs sampled)
effect Result x 26,538,884 ops/sec ±0.43% (96 runs sampled)
Boxed Result x 422,758,104 ops/sec ±0.98% (94 runs sampled)
```

## Future

Careful on the interpretation of the following, as Future doesn't use microtasks and calls its listeners synchronously.

```
Future x 37,910,965 ops/sec ±1.02% (90 runs sampled)
Promise x 12,620,204 ops/sec ±1.74% (83 runs sampled)
Promise x 13,399,997 ops/sec ±0.57% (81 runs sampled)
Future x 210,785 ops/sec ±7.41% (32 runs sampled)
```

## Future with Result

```
fp-ts TaskEither x 538,403 ops/sec ±0.29% (86 runs sampled)
effect Effect x 189,220 ops/sec ±0.94% (87 runs sampled)
Future x 575,626 ops/sec ±0.30% (90 runs sampled)
```
64 changes: 64 additions & 0 deletions benchmark/src/future-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Benchmark = require("benchmark");
const { Future, Result } = require("../../dist/Boxed");
const fp = require("fp-ts");
const effect = require("effect");

const suite = new Benchmark.Suite();

const getRandom = () => Promise.resolve(~~(Math.random() * 2));

suite.add("fp-ts TaskEither", {
defer: true,
fn: (deferred) => {
fp.function
.pipe(
fp.taskEither.tryCatch(getRandom, String),
fp.taskEither.flatMap((x) =>
x % 2 === 0
? fp.taskEither.right(x)
: fp.taskEither.left(new TypeError()),
),
)()
.then((x) => {
deferred.resolve();
});
},
});

suite.add("effect Effect", {
defer: true,
fn: (deferred) => {
effect
.pipe(
effect.Effect.tryPromise(getRandom),
effect.Effect.flatMap((x) =>
x % 2 === 0
? effect.Effect.succeed(x)
: effect.Effect.fail(new TypeError()),
),
effect.Effect.runPromiseExit,
)
.then(() => {
deferred.resolve();
});
},
});

suite.add("Future", {
defer: true,
fn: (deferred) => {
Future.fromPromise(getRandom())
.mapOkToResult((x) =>
x % 2 === 0 ? Result.Ok(x) : Result.Error(new TypeError()),
)
.onResolve(() => {
deferred.resolve();
});
},
});

suite.on("cycle", function (event) {
console.log(String(event.target));
});

suite.run({ async: true });
17 changes: 11 additions & 6 deletions benchmark/src/future.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ const { Future } = require("../../dist/Boxed");

const suite = new Benchmark.Suite();

suite.add("Future", () => {
Future.value(1)
.map((x) => x + 1)
.onResolve((v) => {});
});

suite.add("Promise", {
defer: true,
fn: (deferred) => {
Expand All @@ -20,6 +14,17 @@ suite.add("Promise", {
},
});

suite.add("Future", {
defer: true,
fn: (deferred) => {
Future.value(1)
.map((x) => x + 1)
.onResolve((v) => {
deferred.resolve();
});
},
});

suite.on("cycle", function (event) {
console.log(String(event.target));
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"prepack": "yarn test && yarn build",
"test": "vitest run",
"typecheck": "tsc --noEmit",
"benchmark": "node benchmark/src/option && node benchmark/src/result && node benchmark/src/future"
"benchmark": "node benchmark/src/option && node benchmark/src/result && node benchmark/src/future && node benchmark/src/future-result"
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
Expand Down

0 comments on commit f86aea2

Please sign in to comment.