Skip to content

Commit 039307f

Browse files
Merge pull request #11972 from Snuffleupagus/ChunkedStream-loadedChunks-Set
Change the `loadedChunks` property, on `ChunkedStream` instances, from an Array to a Set
2 parents 891c706 + b7272a3 commit 039307f

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/core/chunked_stream.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class ChunkedStream {
2929
this.pos = 0;
3030
this.end = length;
3131
this.chunkSize = chunkSize;
32-
this.loadedChunks = [];
33-
this.numChunksLoaded = 0;
32+
this._loadedChunks = new Set();
3433
this.numChunks = Math.ceil(length / chunkSize);
3534
this.manager = manager;
3635
this.progressiveDataLength = 0;
@@ -42,7 +41,7 @@ class ChunkedStream {
4241
getMissingChunks() {
4342
const chunks = [];
4443
for (let chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
45-
if (!this.loadedChunks[chunk]) {
44+
if (!this._loadedChunks.has(chunk)) {
4645
chunks.push(chunk);
4746
}
4847
}
@@ -53,6 +52,10 @@ class ChunkedStream {
5352
return [this];
5453
}
5554

55+
get numChunksLoaded() {
56+
return this._loadedChunks.size;
57+
}
58+
5659
allChunksLoaded() {
5760
return this.numChunksLoaded === this.numChunks;
5861
}
@@ -75,10 +78,9 @@ class ChunkedStream {
7578
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
7679

7780
for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
78-
if (!this.loadedChunks[curChunk]) {
79-
this.loadedChunks[curChunk] = true;
80-
++this.numChunksLoaded;
81-
}
81+
// Since a value can only occur *once* in a `Set`, there's no need to
82+
// manually check `Set.prototype.has()` before adding the value here.
83+
this._loadedChunks.add(curChunk);
8284
}
8385
}
8486

@@ -95,10 +97,9 @@ class ChunkedStream {
9597
: Math.floor(position / this.chunkSize);
9698

9799
for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
98-
if (!this.loadedChunks[curChunk]) {
99-
this.loadedChunks[curChunk] = true;
100-
++this.numChunksLoaded;
101-
}
100+
// Since a value can only occur *once* in a `Set`, there's no need to
101+
// manually check `Set.prototype.has()` before adding the value here.
102+
this._loadedChunks.add(curChunk);
102103
}
103104
}
104105

@@ -112,7 +113,7 @@ class ChunkedStream {
112113
return;
113114
}
114115

115-
if (!this.loadedChunks[chunk]) {
116+
if (!this._loadedChunks.has(chunk)) {
116117
throw new MissingDataException(pos, pos + 1);
117118
}
118119
this.lastSuccessfulEnsureByteChunk = chunk;
@@ -130,7 +131,7 @@ class ChunkedStream {
130131
const beginChunk = Math.floor(begin / chunkSize);
131132
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
132133
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
133-
if (!this.loadedChunks[chunk]) {
134+
if (!this._loadedChunks.has(chunk)) {
134135
throw new MissingDataException(begin, end);
135136
}
136137
}
@@ -140,15 +141,15 @@ class ChunkedStream {
140141
const numChunks = this.numChunks;
141142
for (let i = 0; i < numChunks; ++i) {
142143
const chunk = (beginChunk + i) % numChunks; // Wrap around to beginning.
143-
if (!this.loadedChunks[chunk]) {
144+
if (!this._loadedChunks.has(chunk)) {
144145
return chunk;
145146
}
146147
}
147148
return null;
148149
}
149150

150151
hasChunk(chunk) {
151-
return !!this.loadedChunks[chunk];
152+
return this._loadedChunks.has(chunk);
152153
}
153154

154155
get length() {
@@ -286,7 +287,7 @@ class ChunkedStream {
286287
const endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
287288
const missingChunks = [];
288289
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
289-
if (!this.loadedChunks[chunk]) {
290+
if (!this._loadedChunks.has(chunk)) {
290291
missingChunks.push(chunk);
291292
}
292293
}

0 commit comments

Comments
 (0)