oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.13k stars 2.68k forks source link

Significant Performance Degradation in Excel Document Generation from Version 1.1.8 to 1.1.9 #11385

Open dorlanpabon opened 3 months ago

dorlanpabon commented 3 months ago

What version of Bun is running?

1.1.9+bb13798d9

What platform is your computer?

Microsoft Windows NT 10.0.19044.0 x64

What steps can reproduce the bug?

Use Bun.js version 1.1.8 with Hono to generate an Excel document. Measure the time taken for the document to be generated (approximately 2 seconds). Upgrade to Bun.js version 1.1.9 or higher. Generate the same Excel document and measure the time taken (approximately 50 seconds). Code Snippet:

import { Hono } from 'hono';

const app = new Hono();

app.get('/', async (c) => {
  const Excel = require('exceljs');
  const workbook = new Excel.Workbook();
  const sheet = workbook.addWorksheet('Sheet1');

  const init = async () => {
    try {
      console.log('Start');
      sheet.addRow(['Header1', 'Header2', 'Header3']);
      for (let i = 0; i < 51; i++) {
        const row = [];
        for (let j = 0; j < 207; j++) { // Columns A to HB
          row.push(`Data ${i}-${j}`);
        }
        sheet.addRow(row);
        sheet.getRow(i + 2).eachCell({ includeEmpty: true }, function (cell) {
          cell.fill = {
            type: 'pattern',
            pattern: 'solid',
            fgColor: { argb: 'FFFF0000' }
          };
          cell.font = { name: 'Arial', size: 12, bold: true };
        });
      }

      const buffer = await workbook.xlsx.writeBuffer();
      return buffer;
    } catch (error) {
      console.log(error);
    }
  };

  try {
    console.time('init');
    init().then((buffer) => {
      console.timeEnd('init');
      c.json({ message: 'Hello World' });
    });
  } catch (error) {
    console.log(error);
  } finally {
    console.log('End');
  }
  c.json({ message: 'Hello World' });
});

export default app;

What is the expected behavior?

The document generation time should be consistent or improved in newer versions.

What do you see instead?

The document generation time has increased significantly from version 1.1.9 onwards, taking approximately 50 seconds compared to 2 seconds in version 1.1.8.

Additional information

Operating System: Windows Library: "exceljs": "^4.4.0", "hono": "^4.3.11" The process involves populating data and adding styles to columns A to HB and 51 rows. Using Hono with Bun.js. If there are any specific changes or updates in version 1.1.9 or higher that might have caused this, please let me know. I would appreciate any guidance or fixes for this issue.

image

Jarred-Sumner commented 3 months ago

Can you run bun upgrade and try again?

dorlanpabon commented 3 months ago

Thank you for your response.

I have also tried running bun upgrade and tested it with version 1.1.10, and the performance issue persists. The document generation time is still significantly higher compared to version 1.1.8.

Here are the details:

Version 1.1.8: Approximately 2 seconds to generate the Excel document. Version 1.1.10: Approximately 50 seconds to generate the same document.

dorlanpabon commented 3 months ago

I noticed that this performance degradation started with version 1.1.9. Could it be related to this pull request or this pull request which was merged around the same time (5 days before the release of 1.1.9)?

If there's anything else you'd like me to try or additional information you need, please let me know. Thank you for your assistance.

Jarred-Sumner commented 3 months ago

This is an event loop regression. Something is not being ref'd that was previously ref'd. It works after a minute because that's when the event loop woke up next time.

As a very temporary workaround, you can try sticking a setInterval(() => {}, 16) somewhere to force the application to wake up every 16 milliseconds

kravetsone commented 2 months ago

+1 on Windows + Bun@1.1.17

I also came across the fact that Excel JS on Bun generates a document for about 14 seconds when on Node it is less than 71ms

Node.js@20

PerformanceMeasure {
  name: 'some',
  entryType: 'measure',
  startTime: 10377.2243,
  duration: 71.45349999999962
}

vs

Bun@1.1.17

PerformanceMeasure {
  detail: null,
  name: "some",
  entryType: "measure",
  startTime: 7292.4023,
  duration: 14305.9681,
  toJSON: [Function: toJSON],
}