Switching deno fmt from prettier to dprint, performance increase 10+ times

If you ask what Deno's release brings, the most important thing is undoubtedly bringing the high-performance Rust toolchain to the front end.

Mention JavaScript always gives people a feeling: slow.

nodejs v8 deno

Deno is developed using TypeScript and Rust. In addition to running JS(X) and TS(X) code, Deno also provides other command line parameters. For example  deno fmtdeno lintdeno doc and so on. Today our topic is  deno fmt: code formatting tools.

deno fmt --help The output of the run  :

deno-fmt
Auto-format JavaScript/TypeScript source code.
  deno fmt
  deno fmt myfile1.ts myfile2.ts
  deno fmt --check

Format stdin and write to stdout:
  cat file.ts | deno fmt -

Ignore formatting code by preceding it with an ignore comment:
  // deno-fmt-ignore

Ignore formatting a file by adding an ignore comment at the top of the file:
  // deno-fmt-ignore-file

USAGE:
    deno fmt [OPTIONS] [files]...

OPTIONS:
        --check
            Check if the source files are formatted.

    -h, --help
            Prints help information

    -L, --log-level <log-level>
            Set log level [possible values: debug, info]

    -q, --quiet
            Suppress diagnostic output
            By default, subcommands print human-readable diagnostic messages to stderr.
            If the flag is set, restrict these messages to errors.

ARGS:
    <files>...

In the initial version, the Deno formatting tool used prettier. Prettier is a very popular code formatting tool on the front end, with 30 to 40 million monthly downloads on npm.

Not long ago, Deno's formatting tool switched to dprint. About a year ago (June 2019) David Sherret started developing dprint, initially as a Node.js project. Starting this year, the author migrated all dprint code to Rust.

dprint cli itself does not deal with file formatting. The operation of formatting specific types of files is provided by plug-ins. Currently, there are four official plug-ins that can be used to format ts/js, json, rust, and markdown. These plugins are also written in Rust and compiled into wasm and crate (crate is a package of Rust).

All plug-ins of dprint are in wasm format, while deno fmt uses crate directly, so deno fmt is better than dprint in terms of performance.

I tested a TypeScript React project with 590 files on my computer:

Formatted 590 files.
dprint fmt 7.82s user 3.05s system 326% cpu  3.334 total
deno fmt   4.41s user 2.49s system 748% cpu  0.921 total
prettier  15.77s user 4.28s system 124% cpu 16.042 total

It can be seen that dprint is 5 times that of prettier and deno fmt is 17 times that of prettier.

Currently dprint is still in rapid iteration, and more formatting plugins will be developed in the future, so that more file types can be formatted faster, such as vue, css, pug, etc.

I have already planned to use deno fmt instead of prettier in my next React project.

In addition to deno fmt, deno lint version 0.1.19 has also been released. The benchmark data shows that the performance of deno_lint is about 14 times that of eslint.

Guess you like

Origin blog.csdn.net/vCa54Lu0KV27w8ZZBd/article/details/107678625