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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zipping files is extremely slow with archiver or jszip #12397

Open
mnapoli opened this issue Jul 6, 2024 · 9 comments
Open

Zipping files is extremely slow with archiver or jszip #12397

mnapoli opened this issue Jul 6, 2024 · 9 comments
Assignees
Labels
bug Something isn't working node:zlib

Comments

@mnapoli
Copy link

mnapoli commented Jul 6, 2024

What version of Bun is running?

1.1.18+5a0b93523

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

  • use [jszip](https://github.com/Stuk/jszip) or archiver
  • create a zip with 10k files (13442 in my test)

Rough script:

var fs = require("fs");
var JSZip = require("jszip");

var zip = new JSZip();
// Call this 10k times on 10k files
// zip.file(path, fs.readFileAsync(path));

zip
.generateNodeStream({type:'nodebuffer',streamFiles:true})
.pipe(fs.createWriteStream('out.zip'))
.on('finish', function () {
    console.log("out.zip written.");
});

What is the expected behavior?

The script should complete in a couple of seconds. That's what happens with Node.

What do you see instead?

The script is extremely slow: if I log progress, I see 0.01% to 0.1% progress every second. Needless to say the script would complete in several minutes or possibly hours.

Additional information

I tried both archiver and jszip, hoping the problem was specific to an implementation. No difference.

I also tried replacing fs.readFileAsync(path) with Bun.file(path).blob() but it made no difference. Loading all files in memory and adding them to the zip instance takes a second at most (including with Bun).

The time is then spent by the zipping method.

@whekin
Copy link

whekin commented Jul 6, 2024

I experienced a similar issue. I had to switch the library to PIzzip, which is a synchronous fork of the library and it worked okay. I'd also add that I worked with small docx documents, and while Node took a couple of dozens milliseconds to generate a buffer, it took 30x more time in Bun (around 1-1.5 seconds)

@ghiscoding
Copy link

see old PR #11770

@nektro
Copy link
Member

nektro commented Oct 8, 2024

does this still reproduce for you in Bun 1.1.29?

@whekin
Copy link

whekin commented Oct 8, 2024

@nektro I haven't noticed any changes. JSZip is still significantly (10x+ times) slower for me. When I switch to Pizzip it's fast.
I'm using export method from here https://github.com/alonrbar/easy-template-x/blob/master/src/zip/zip.ts. It uses generateAsync of JSZip in the method

@nektro nektro self-assigned this Oct 8, 2024
@whekin
Copy link

whekin commented Oct 8, 2024

@nektro I'm trying to reproduce the issue outside of my project and no success so far, it works good. I'm using easy-template-x same way in my test and in my project. But performance in my project is horrible (it can take 100-500x to export/setFile compared to test). I'm trying to narrow the issue. It happens only in Bun though, in Node there's no difference

@whekin
Copy link

whekin commented Oct 8, 2024

@nektro I could narrow it down to the point that some import makes it suddenly much slower. However I couldn't find what exact import

@fructiferous
Copy link

@whekin, @nektro, I had similar issues attempting to isolate and debug this earlier this year. I managed to narrow it down to my code running in the context of an HTTP server (or the official Bun lambda runtime), which a full project is likely to feature, but one tends to omit in a standalone test with a CLI script. See: #11054 for the code to reproduce this as well as a benchmark script + results, it might help either of you. Also note: I just tested v1.1.31 and the slowdown is still present.

@Jarred-Sumner
Copy link
Collaborator

The issue is caused by us draining tasks from setImmediate only once at the beginning of the event loop. This is more frequently an issue when the HTTP server is responding to a lot of socket activity

@GautierT
Copy link

Hi !
Is there a fix in the roadmap ?
Thanks a lot.

@Jarred-Sumner Jarred-Sumner assigned 190n and unassigned nektro Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node:zlib
Projects
None yet
Development

No branches or pull requests

8 participants