Download ZIP deflates sixteen files at a time, within a Worker's memory
1 file+11−20/1 viewed
| 32 | 32 | const parts: Uint8Array[] = []; | |
| 33 | 33 | const central: Uint8Array[] = []; | |
| 34 | 34 | let offset = 0; | |
| 35 | − | // Every file deflated at once: the streams interleave rather than queue. | |
| 36 | − | const deflatedAll = await Promise.all(files.map((file) => (file.data.length > 0 ? deflate(file.data) : file.data))); | |
| 35 | + | // Sixteen files deflating at a time: quicker than one by one, without | |
| 36 | + | // every file's stream held at once. | |
| 37 | + | const deflatedAll: Uint8Array[] = new Array(files.length); | |
| 38 | + | let next = 0; | |
| 39 | + | const worker = async () => { | |
| 40 | + | for (let index = next++; index < files.length; index = next++) { | |
| 41 | + | const data = files[index]!.data; | |
| 42 | + | deflatedAll[index] = data.length > 0 ? await deflate(data) : data; | |
| 43 | + | } | |
| 44 | + | }; | |
| 45 | + | await Promise.all(Array.from({ length: Math.min(16, files.length) }, worker)); | |
| 37 | 46 | for (const [index, file] of files.entries()) { | |
| 38 | 47 | const name = encoder.encode(file.path); | |
| 39 | 48 | const crc = crc32(file.data); |