Skip to content

Commit

Permalink
refactor: fixing code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Nov 7, 2024
1 parent 9ed2876 commit 0c626a8
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 175 deletions.
22 changes: 5 additions & 17 deletions src/recipes/branch-recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface LinkedNode<T> {
function getBranchedAsyncIterable<T>(
node: LinkedNode<T> | undefined,
iterator: AsyncIterator<T>,
context: { next?: Promise<IteratorResult<T>> },
context: { next?: Promise<void> },
) {
return {
[Symbol.asyncIterator]() {
Expand All @@ -21,18 +21,10 @@ function getBranchedAsyncIterable<T>(
value = node.value;
done = false;
if (!node.next) {
const promise = (context.next ??= new Promise(
async (resolve, reject) => {
try {
const next = await iterator.next();
if (!next.done) node!.next = { value: next.value };
context.next = undefined;
resolve(next);
} catch (err) {
reject(err);
}
},
));
const promise = (context.next ??= iterator.next().then((next) => {
if (!next.done) node!.next = { value: next.value };
context.next = undefined;
}));
return promise.then(() => {
node = node!.next;
return { done, value };
Expand Down Expand Up @@ -63,10 +55,6 @@ function getBranchedIterable<T>(
if (!node.next) {
const next = iterator.next();
if (!next.done) node.next = { value: next.value };
return new Promise((resolve) => {
node = node!.next;
resolve({ done, value });
});
}
node = node.next;
}
Expand Down
22 changes: 8 additions & 14 deletions test/benchmark/branch-benchmark.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ function* interval(init, final) {
}

let log = '';
let min = [];
let max = [];
let avg = [];
let minBranch = [];
let maxBranch = [];
let avgBranch = [];
const errors = new Set();
function getBase() {
return fluent(interval(1, ITEMS))
Expand All @@ -30,18 +24,18 @@ function getBase() {

benchmarkSuite
.add('separated', () => {
min = getBase().min();
max = getBase().max();
avg = getBase().avg();
getBase().min();
getBase().max();
getBase().avg();
})
.add('manual branch', () => {
base = getBase().toArray();
min = fluent(base).min();
max = fluent(base).max();
avg = fluent(base).avg();
const base = getBase().toArray();
fluent(base).min();
fluent(base).max();
fluent(base).avg();
})
.add('banching', async () => {
[minBranch, maxBranch, avgBranch] = await getBase()
await getBase()
.branch(
x => x.min(),
x => x.max(),
Expand Down
Loading

0 comments on commit 0c626a8

Please sign in to comment.