Skip to content

Commit c99aede

Browse files
timursevimlitshemsedinov
authored andcommitted
Optimise while expression
1 parent 0d8bc16 commit c99aede

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

JavaScript/6-stream.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
const { Readable } = require('node:stream');
44

55
class QueueStream extends Readable {
6-
constructor(concurrent) {
6+
constructor(concurrency) {
77
super({ objectMode: true });
8-
this.concurrent = concurrent;
8+
this.concurrency = concurrency;
99
this.count = 0;
10-
this.queue = [];
10+
this.waiting = [];
1111
}
1212

13-
static channels(concurrent) {
14-
return new QueueStream(concurrent);
13+
static channels(concurrency) {
14+
return new QueueStream(concurrency);
1515
}
1616

1717
add(task) {
18-
this.queue.push(task);
18+
this.waiting.push(task);
1919
}
2020

2121
_read() {
22-
while (this.count < this.concurrent && this.queue.length > 0) {
23-
const task = this.queue.shift();
22+
const emptyChannels = this.concurrency - this.count;
23+
let launchCount = Math.min(emptyChannels, this.waiting.length);
24+
while (launchCount-- > 0) {
2425
this.count++;
26+
const task = this.waiting.shift();
2527
this.onProcess(task, (err, res) => {
2628
if (err) this.emit('error', err);
2729
this.push({ err, res });
2830
this.count--;
2931
});
3032
}
31-
if (this.queue.length === 0 && this.count === 0) {
33+
if (this.waiting.length === 0 && this.count === 0) {
3234
this.push(null);
3335
}
3436
}

JavaScript/7-pipeline.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
const { Readable, Writable, Transform, pipeline } = require('node:stream');
44

55
class QueueStream extends Readable {
6-
constructor(concurrent) {
6+
constructor(concurrency) {
77
super({ objectMode: true });
8-
this.concurrent = concurrent;
8+
this.concurrency = concurrency;
99
this.count = 0;
10-
this.queue = [];
10+
this.waiting = [];
1111
}
1212

13-
static channels(concurrent) {
14-
return new QueueStream(concurrent);
13+
static channels(concurrency) {
14+
return new QueueStream(concurrency);
1515
}
1616

1717
add(task) {
18-
this.queue.push(task);
18+
this.waiting.push(task);
1919
}
2020

2121
_read() {
22-
while (this.count < this.concurrent && this.queue.length > 0) {
23-
const task = this.queue.shift();
22+
const emptyChannels = this.concurrency - this.count;
23+
let launchCount = Math.min(emptyChannels, this.waiting.length);
24+
while (launchCount-- > 0) {
25+
const task = this.waiting.shift();
2426
this.count++;
2527
this.onProcess(task, (err, res) => {
2628
if (err) this.emit('error', err);
2729
this.push({ err, res });
2830
this.count--;
2931
});
3032
}
31-
if (this.queue.length === 0 && this.count === 0) {
33+
if (this.waiting.length === 0 && this.count === 0) {
3234
this.push(null);
3335
}
3436
}

0 commit comments

Comments
 (0)