denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.45k stars 5.24k forks source link

Deno fmt performance issue on minified files with excessive nested assignments #14561

Open sigmaSd opened 2 years ago

sigmaSd commented 2 years ago

deno fmt seems to exhaust memory and not finish when used on certain files

to reproduce: (!careful this crashes my pc)

In contrast using https://esm.sh/js-beautify finish almost immediately

await import("https://esm.sh/js-beautify").then(async (f)=>f.default.js_beautify(await fetch("https://github.com/github/copilot.vim/raw/release/copilot/dist/agent.js").then(t=>t.text())))
LumaKernel commented 1 year ago

I found a simpler pattern in type only while original version is JS only.

type x = {"x":[{"x":[{"x":[{"x":[{"x":[{"x":[{"x":[{"x":[{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]},{'x':[{'x':'x'}]}]}]}]}]}]}]}]}]}

Save as typescript file and run fmt, it leads super memory consuming. (maybe at least around 8GB or more somehow) It reaches maximum memory usage in a few seconds.

Using version deno 1.28.1 (release, x86_64-unknown-linux-gnu).

Expected form is like following. This can be formatted soon:

type x = {
  "x": [
    {
      "x": [
        {
          "x": [
            {
              "x": [
                {
                  "x": [
                    {
                      "x": [
                        {
                          "x": [
                            {
                              "x": [
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                                { "x": [{ "x": "x" }] },
                              ];
                            },
                          ];
                        },
                      ];
                    },
                  ];
                },
              ];
            },
          ];
        },
      ];
    },
  ];
};

Increasing nest or number of last repeating { "x": [{ "x": "x" }] }, makes it more memory consuming.