Skip to content

Commit 135fb49

Browse files
committed
chore: add all benchmark
1 parent afda186 commit 135fb49

File tree

4 files changed

+65
-22
lines changed

4 files changed

+65
-22
lines changed

benchmark.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
Running benchmark...
2-
clk: ~3.14 GHz
2+
clk: ~3.03 GHz
33
cpu: Apple M1 Max
44
runtime: node 24.11.1 (arm64-darwin)
55

66
| benchmark | avg | min | p75 | p99 | max |
77
| ----------------- | ---------------- | ----------- | ----------- | ----------- | ----------- |
8-
| sync: native fn | `280.17 ps/iter` | `254.15 ps` | `284.67 ps` | `315.19 ps` | ` 10.48 ns` |
9-
| sync: quansync fn | `282.21 ps/iter` | `254.15 ps` | `284.91 ps` | `315.43 ps` | ` 10.23 ns` |
10-
| sync: gensync fn | ` 57.44 ns/iter` | ` 55.09 ns` | ` 56.40 ns` | ` 86.51 ns` | `114.61 ns` |
8+
| sync: native fn | `295.49 ps/iter` | `254.15 ps` | `294.92 ps` | `335.69 ps` | `445.29 ns` |
9+
| sync: quansync fn | `283.09 ps/iter` | `254.15 ps` | `284.91 ps` | `315.43 ps` | ` 9.81 ns` |
10+
| sync: gensync fn | ` 57.56 ns/iter` | ` 54.51 ns` | ` 56.78 ns` | ` 88.58 ns` | `124.34 ns` |
1111

1212
| | avg | min | p75 | p99 | max |
1313
| ---------------------- | ---------------- | ----------- | ----------- | ----------- | ----------- |
14-
| sync: native add | `281.28 ps/iter` | `254.15 ps` | `284.91 ps` | `315.19 ps` | ` 10.63 ns` |
15-
| sync: native await add | ` 93.01 ns/iter` | ` 89.52 ns` | ` 92.49 ns` | `122.18 ns` | `163.55 ns` |
16-
| sync: quansync add | ` 84.13 ns/iter` | ` 81.42 ns` | ` 83.11 ns` | `112.60 ns` | `237.40 ns` |
17-
| sync: gensync add | ` 86.55 ns/iter` | ` 84.06 ns` | ` 85.73 ns` | `113.88 ns` | `134.39 ns` |
14+
| sync: native add | `284.97 ps/iter` | `254.15 ps` | `284.91 ps` | `315.43 ps` | ` 10.82 ns` |
15+
| sync: native await add | `110.48 ns/iter` | `102.14 ns` | `111.00 ns` | `154.01 ns` | `207.21 ns` |
16+
| sync: quansync add | ` 85.88 ns/iter` | ` 81.37 ns` | ` 85.85 ns` | `121.39 ns` | `229.70 ns` |
17+
| sync: gensync add | ` 96.32 ns/iter` | ` 83.77 ns` | ` 88.44 ns` | `287.93 ns` | ` 1.44 µs` |
1818

1919
| | avg | min | p75 | p99 | max |
2020
| ------------------ | ---------------- | ----------- | ----------- | ----------- | ----------- |
21-
| async: native fn | ` 94.24 ns/iter` | ` 90.55 ns` | ` 93.73 ns` | `122.47 ns` | `168.51 ns` |
22-
| async: quansync fn | ` 91.11 ns/iter` | ` 87.60 ns` | ` 89.86 ns` | `119.48 ns` | `162.91 ns` |
23-
| async: gensync fn | `250.32 ns/iter` | `236.61 ns` | `252.04 ns` | `291.00 ns` | `398.18 ns` |
21+
| sync: quansync.all | `379.39 ns/iter` | `332.16 ns` | `392.44 ns` | `791.99 ns` | `959.25 ns` |
22+
| sync: gensync add | `224.20 ns/iter` | `205.65 ns` | `221.67 ns` | `535.21 ns` | `790.36 ns` |
23+
24+
| | avg | min | p75 | p99 | max |
25+
| ------------------ | ---------------- | ----------- | ----------- | ----------- | ----------- |
26+
| async: native fn | `106.02 ns/iter` | ` 97.18 ns` | `107.49 ns` | `150.01 ns` | `170.93 ns` |
27+
| async: quansync fn | `103.28 ns/iter` | ` 95.70 ns` | `104.38 ns` | `141.51 ns` | `181.34 ns` |
28+
| async: gensync fn | `253.97 ns/iter` | `240.30 ns` | `255.65 ns` | `301.01 ns` | `452.58 ns` |
2429

2530
| | avg | min | p75 | p99 | max |
2631
| ------------------- | ---------------- | ----------- | ----------- | ----------- | ----------- |
27-
| async: native add | `129.41 ns/iter` | `124.74 ns` | `128.35 ns` | `157.93 ns` | `191.80 ns` |
28-
| async: quansync add | `298.89 ns/iter` | `287.86 ns` | `300.44 ns` | `335.28 ns` | `383.16 ns` |
29-
| async: gensync add | `291.93 ns/iter` | `278.49 ns` | `290.20 ns` | `348.91 ns` | `494.16 ns` |
32+
| async: native add | `141.76 ns/iter` | `134.77 ns` | `141.25 ns` | `176.71 ns` | `205.23 ns` |
33+
| async: quansync add | `315.35 ns/iter` | `299.40 ns` | `322.45 ns` | `367.67 ns` | `388.60 ns` |
34+
| async: gensync add | `314.88 ns/iter` | `291.43 ns` | `323.03 ns` | `373.04 ns` | `504.60 ns` |
35+
36+
| | avg | min | p75 | p99 | max |
37+
| ------------------------- | ---------------- | ----------- | ----------- | ----------- | ----------- |
38+
| async: native Promise.all | `250.69 ns/iter` | `237.47 ns` | `254.38 ns` | `300.04 ns` | `310.30 ns` |
39+
| async: quansync.all | `985.55 ns/iter` | `922.67 ns` | ` 1.01 µs` | ` 1.08 µs` | ` 1.35 µs` |
40+
| async: gensync add | ` 1.07 µs/iter` | `589.54 ns` | ` 1.93 µs` | ` 2.25 µs` | ` 2.68 µs` |

scripts/benchmark.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import gensync from 'gensync'
33
import { bench, do_not_optimize, run, summary } from 'mitata'
44
// eslint-disable-next-line antfu/no-import-dist
5-
import { quansync } from '../dist/index.js'
5+
import { all, quansync } from '../dist/index.js'
66

77
const sync = () => 10
88
const async = async () => 10
99
const addNativeSync = /** @param {number} n */ n => sync() + n
1010
const addNativeAsync = /** @param {number} n */ async n => await async() + n
11+
const nativeAll = () => Promise.all([async(), async()])
1112

1213
const quansyncFn = quansync({
1314
sync: () => 10,
@@ -16,6 +17,9 @@ const quansyncFn = quansync({
1617
const quansyncAdd = quansync(/** @param {number} n */ function* (n) {
1718
return (yield* quansyncFn()) + n
1819
})
20+
const quansyncAll = quansync(function* () {
21+
return all([quansyncFn(), quansyncFn()])
22+
})
1923

2024
const gensyncFn = gensync({
2125
sync: () => 10,
@@ -24,6 +28,9 @@ const gensyncFn = gensync({
2428
const gensyncAdd = gensync(/** @param {number} n */ function* (n) {
2529
return (yield* gensyncFn()) + n
2630
})
31+
const gensyncAll = gensync(function* () {
32+
return yield* gensync.all([gensyncFn(), gensyncFn()])
33+
})
2734

2835
summary(() => {
2936
bench('sync: native fn', function* () {
@@ -52,6 +59,15 @@ summary(() => {
5259
})
5360
})
5461

62+
summary(() => {
63+
bench('sync: quansync.all', function* () {
64+
yield () => do_not_optimize(quansyncAll.sync())
65+
})
66+
bench('sync: gensync add', function* () {
67+
yield () => do_not_optimize(gensyncAll.sync())
68+
})
69+
})
70+
5571
summary(() => {
5672
bench('async: native fn', function* () {
5773
yield async () => do_not_optimize(await async())
@@ -76,6 +92,18 @@ summary(() => {
7692
})
7793
})
7894

95+
summary(() => {
96+
bench('async: native Promise.all', function* () {
97+
yield async () => do_not_optimize(await nativeAll())
98+
})
99+
bench('async: quansync.all', function* () {
100+
yield async () => do_not_optimize(await quansyncAll.async())
101+
})
102+
bench('async: gensync add', function* () {
103+
yield async () => do_not_optimize(await gensyncAll.async())
104+
})
105+
})
106+
79107
console.info('Running benchmark...')
80108
await run({
81109
format: 'markdown',

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function fromObject<Return, Args extends any[]>(
3535
}
3636
fn.sync = options.sync
3737
fn.async = options.async
38-
return fn
38+
return fn as any
3939
}
4040

4141
function fromPromise<T>(promise: PromiseLike<T> | T): QuansyncFn<T, []> {
@@ -136,7 +136,7 @@ export function quansync<Return, Args extends any[] = []>(
136136
export function toGenerator<T>(promise: PromiseLike<T> | QuansyncGenerator<T> | T): QuansyncGenerator<T> {
137137
if (isQuansyncGenerator(promise))
138138
return promise
139-
return fromPromise(promise)()
139+
return fromPromise(promise)() as any
140140
}
141141

142142
/**

src/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ export type QuansyncGenerator<Return = any, Yield = unknown>
2121
export type QuansyncAwaitableGenerator<Return = any, Yield = unknown>
2222
= QuansyncGenerator<Return, Yield> & PromiseLike<Return>
2323

24+
export type QuansyncUnwrapGenerator<T> = T extends QuansyncGenerator<infer R, any>
25+
? R
26+
: T
27+
2428
/**
2529
* "Superposition" function that can be consumed in both sync and async contexts.
2630
*/
2731
export type QuansyncFn<Return = any, Args extends any[] = []>
28-
= ((...args: Args) => QuansyncAwaitableGenerator<Return>)
32+
= ((...args: Args) => QuansyncAwaitableGenerator<QuansyncUnwrapGenerator<Return>>)
2933
& {
3034
/**
3135
* **Warning**: The `async` and `sync` methods will be lost after invoked.
3236
*/
3337
bind: <T, A extends any[], B extends any[]>(
34-
this: (this: T, ...args: [...A, ...B]) => QuansyncAwaitableGenerator<Return>,
38+
this: (this: T, ...args: [...A, ...B]) => QuansyncAwaitableGenerator<QuansyncUnwrapGenerator<Return>>,
3539
thisArg: T,
3640
...args: A
37-
) => ((...args: B) => QuansyncAwaitableGenerator<Return>)
41+
) => ((...args: B) => QuansyncAwaitableGenerator<QuansyncUnwrapGenerator<Return>>)
3842

39-
sync: (...args: Args) => Return
40-
async: (...args: Args) => Promise<Return>
43+
sync: (...args: Args) => QuansyncUnwrapGenerator<Return>
44+
async: (...args: Args) => Promise<QuansyncUnwrapGenerator<Return>>
4145
}

0 commit comments

Comments
 (0)