Skip to content

Commit

Permalink
Added benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Sep 18, 2024
1 parent 57df5c6 commit 9f983ee
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bench/gen-fun.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import gen from 'stream-chain/gen.js';
import fun from 'stream-chain/fun.js';
import {getManyValues} from 'stream-chain/defs.js';

const fns = [x => x - 2, x => x + 1, x => 2 * x, x => x + 2, x => x >> 1];

const g = gen(...fns),
f = fun(...fns);

export default {
async gen(n) {
let acc = 0;
for (let i = 0; i < n; ++i) {
for await (const x of g(i)) {
acc += x;
}
}
return acc;
},
async fun(n) {
let acc = 0;
for (let i = 0; i < n; ++i) {
for (const x of getManyValues(await f(i))) {
acc += x;
}
}
return acc;
}
};
60 changes: 60 additions & 0 deletions bench/gen-opt.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import gen from 'stream-chain/gen.js';
import {clearFunctionList} from 'stream-chain/defs.js';

const g1 = gen(
x => x - 2,
x => x + 1,
x => 2 * x,
x => x + 2,
x => x >> 1
),
g2 = gen(
x => x - 2,
gen(
x => x + 1,
x => 2 * x,
x => x + 2
),
x => x >> 1
),
g3 = gen(
x => x - 2,
clearFunctionList(
gen(
x => x + 1,
x => 2 * x,
x => x + 2
)
),
x => x >> 1
);

export default {
async ['simple list'](n) {
let acc = 0;
for (let i = 0; i < n; ++i) {
for await (const x of g1(i)) {
acc += x;
}
}
return acc;
},
async ['optimization on'](n) {
let acc = 0;
for (let i = 0; i < n; ++i) {
for await (const x of g2(i)) {
acc += x;
}
}
return acc;
},
async ['optimization off'](n) {
let acc = 0;
for (let i = 0; i < n; ++i) {
for await (const x of g3(i)) {
acc += x;
}
}
return acc;
}
};
41 changes: 41 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"@types/node": "^22.5.5",
"nano-benchmark": "^1.0.2",
"tape-six": "^0.9.6",
"typescript": "^5.6.2"
}
Expand Down

0 comments on commit 9f983ee

Please sign in to comment.