oak icon indicating copy to clipboard operation
oak copied to clipboard

Form Data (with file) parsing slow

Open kaleidawave opened this issue 3 years ago • 3 comments

I am finding that parsing form data with files is very slow. Some crude testing but oak form data parsing seems to be ~75x times longer than express's multer

With oak parsing form data with a single short string field and a 1.6mb jpeg image takes ~7500ms. On the other hand multer parses the ~100ms

Oak Deno Test:

import { Application } from "../application.ts";
import { join } from "https://deno.land/[email protected]/path/posix.ts";
import { Status } from "https://deno.land/[email protected]/http/http_status.ts";

const app = new Application();

app.addEventListener("listen", console.log);

app.use(async ctx => {
    console.time("Read file");
    const formDataReader = ctx.request.body({type: "form-data"});
    console.timeLog("Read file", "Get reader");
    const formData = await formDataReader.value.read({ outPath: join(Deno.cwd(), "out") });
    console.timeEnd("Read file");
    ctx.response.status = Status.Created;
});

await app.listen({ port: 8080 });

NodeJS Express Multer Test:

const express = require("express");
const multer = require("multer");

const upload = multer({ dest: 'uploads/' });

const app = express();

app.post("/", (req, res, next) => {
    console.time("Read file");
    next();
}, upload.single("File1"), (req, res) => {
    console.timeEnd("Read file");
    res.sendStatus(201);
});

app.listen(8080, console.log);

kaleidawave avatar Sep 23 '20 10:09 kaleidawave

I'm not so familiar with oaks code but I think it is because of the many loops which are used to parse. Also for await can get really slow when it iterates over something big so that might be an issue too

itohatweb avatar Jul 01 '21 21:07 itohatweb

Bump ^^ Currently it takes about 2 seconds to read a 7.7mb file. Would be nice if it could be fixed soon .-.

itohatweb avatar Aug 27 '21 14:08 itohatweb

The biggest challenge is https://github.com/denoland/deno/issues/10157. Deno is adding a benchmark to try to focus on improving the performance (see: https://github.com/denoland/deno/pull/11862). I will try to take a look at some point if there are additional challenges in oak, but the big bottleneck at the moment is Deno.

kitsonk avatar Aug 27 '21 21:08 kitsonk