Skip to content

doc: update stream.reduce concurrency note - #47166

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
rluvaton:update-stream-operator-reduce-docs
Mar 24, 2023
Merged

doc: update stream.reduce concurrency note#47166
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
rluvaton:update-stream-operator-reduce-docs

Conversation

@rluvaton

Copy link
Copy Markdown
Member

At first, when I read that note I thought I should do this:

import{Readable}from'node:stream';consttotalViews=awaitReadable.from(videosUrl).map((url)=>url,{concurrency: 2}).reduce(async(total,url)=>{constviews=awaitgetViewsAsync(url)total+views},0);console.log(totalViews);

This did not work and does not make any sense as the map items are emitted in the order that got from the readable stream.

So I thought of changing to note to recommend implementing reduce using .forEach with concurrency but then I understand what @benjamingr meant when he wrote that note - because the only way you will need concurrency is if you do some async operation (because sync block the loop - bla bla) and that operation can be parallelized so I updated the note with an example

@benjamingr, hope this is what you meant 😅

@nodejs-github-botnodejs-github-bot added doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem. labels Mar 19, 2023
@rluvaton
rluvatonforce-pushed the update-stream-operator-reduce-docs branch from 2a31082 to 4b92bfdCompareMarch 19, 2023 21:18
Comment threaddoc/api/stream.md Outdated
Comment threaddoc/api/stream.md Outdated
@rluvaton
rluvatonforce-pushed the update-stream-operator-reduce-docs branch 2 times, most recently from 2ac4139 to 487f5dbCompareMarch 19, 2023 22:58
@rluvaton
rluvatonforce-pushed the update-stream-operator-reduce-docs branch from 487f5db to 9627c88CompareMarch 20, 2023 00:17
Comment threaddoc/api/stream.md
Comment on lines 2508 to +2523
```mjs
import { Readable } from 'node:stream';
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';

const ten = await Readable.from([1, 2, 3, 4]).reduce((previous, data) => {
return previous + data;
});
console.log(ten); // 10
const directoryPath = './src';
const filesInDir = await readdir(directoryPath);

const folderSize = await Readable.from(filesInDir)
.reduce(async (totalSize, file) => {
const { size } = await stat(join(directoryPath, file));
return totalSize + size;
}, 0);

console.log(folderSize);
```

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this example as well so the refactor can be more obvious

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollinamcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 21, 2023
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 21, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@rluvaton

Copy link
Copy Markdown
MemberAuthor

Hey @VoltrexKeyva , can you please rereview so it can be merged?

@rluvaton

Copy link
Copy Markdown
MemberAuthor

Thanks @VoltrexKeyva , can you merge it?

@VoltrexKeyvaVoltrexKeyva added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 24, 2023
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 24, 2023
@nodejs-github-bot
nodejs-github-bot merged commit c588145 into nodejs:mainMar 24, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in c588145

@rluvaton
rluvaton deleted the update-stream-operator-reduce-docs branch March 24, 2023 14:42
RafaelGSS pushed a commit that referenced this pull request Apr 5, 2023
PR-URL: #47166
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
@RafaelGSSRafaelGSS mentioned this pull request Apr 6, 2023
RafaelGSS pushed a commit that referenced this pull request Apr 6, 2023
PR-URL: #47166
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
RafaelGSS pushed a commit that referenced this pull request Apr 7, 2023
PR-URL: #47166
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
danielleadams pushed a commit that referenced this pull request Jul 6, 2023
PR-URL: #47166
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docIssues and PRs related to the documentations.streamIssues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@rluvaton@nodejs-github-bot@mcollina@VoltrexKeyva