Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: update stream.reduce concurrency note #47166

Merged

Conversation

rluvaton
Copy link
Member

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

import { Readable } from 'node:stream';

const totalViews = await Readable.from(videosUrl)
  .map((url) => url, { concurrency: 2 })
  .reduce(async (total, url) => {
    const views = await getViewsAsync(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-bot nodejs-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 rluvaton force-pushed the update-stream-operator-reduce-docs branch from 2a31082 to 4b92bfd Compare March 19, 2023 21:18
doc/api/stream.md Outdated Show resolved Hide resolved
doc/api/stream.md Outdated Show resolved Hide resolved
@rluvaton rluvaton force-pushed the update-stream-operator-reduce-docs branch 2 times, most recently from 2ac4139 to 487f5db Compare March 19, 2023 22:58
@rluvaton rluvaton force-pushed the update-stream-operator-reduce-docs branch from 487f5db to 9627c88 Compare March 20, 2023 00:17
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
Member Author

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

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 21, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 21, 2023
@nodejs-github-bot
Copy link
Collaborator

@rluvaton
Copy link
Member Author

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

@rluvaton
Copy link
Member Author

Thanks @VoltrexKeyva , can you merge it?

@VoltrexKeyva VoltrexKeyva added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 24, 2023
@nodejs-github-bot nodejs-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:main Mar 24, 2023
19 checks passed
@nodejs-github-bot
Copy link
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>
@RafaelGSS RafaelGSS 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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants