Compiler: How is swc faster than babel?

Preface

Both swc and babel are JavaScript compilers. Their main function is to convert ES2015+ and TypeScript, Flow, JSX and other syntaxes into backward-compatible JavaScript code in the browser or environment.

Where is it faster?

1. Advantages of development languages

swc is developed in Rust language, while babel is developed in JavaScript language. This means that swc can take advantage of Rust's performance advantages, such as no GC (garbage collection mechanism), system-level language, multi-core CPU support, etc., while babel is limited by JavaScript performance issues, such as GC, single-threading, etc.

2. AST tree transparent transmission

swc does not need to transparently pass the AST tree when compiling the code, but directly converts it in memory. This can avoid multiple traversals of the AST tree and improve compilation efficiency. Babel needs to pass the AST tree to each plug-in, which will increase compilation time and memory consumption.

3. Compiled code is faster

swc can compile JavaScript code into WebAssembly, which can improve the execution speed of the code. Babel, on the other hand, can only be compiled into JavaScript code and cannot take advantage of WebAssembly.

Summarize

  • The advantage of swc compared to babel is mainly in compilation speed and execution speed. According to official tests, swc has at least 10 times the performance advantage. However, babel's advantages mainly lie in the plug-in system and ecology. Babel has more plug-in and framework support and can handle more complex JavaScript projects.
  • At present, swc has been adopted by front-end frameworks such as Next.js, and is expected to play a greater role in the field of front-end engineering in the future. Babel is still the most popular JavaScript compiler and is widely used by frameworks such as Vue, React, and Angular.

/------------------------------------------------- - Expand knowledge------------------------------------------------- -------/

Why Rust has no GC?

It uses a mechanism called ownership to manage memory

  • The basic idea of ​​ownership : Each value has a unique owner, and when the owner leaves the scope, the value is automatically released. This avoids memory leaks and dangling pointer problems, and eliminates the need for a runtime garbage collector to track and clean up memory.

Rust also introduces the concepts of borrowing and lifetime to control access to and modification of values. Borrowing is the temporary use of a reference to a value without owning it. Lifecycle is the period of time that a value or a reference exists in the program. The Rust compiler checks whether borrowing and lifetime conform to some rules to ensure memory safety and data consistency.

Rust's memory management method can not only ensure the correctness of the program, but also improve the performance of the program because it does not require additional memory overhead and garbage collection pauses. This is one of the reasons why Rust is called "zero-cost abstraction".

system level language

System-level languages ​​refer to those programming languages ​​that can directly operate the hardware and underlying systems.

They usually have the following characteristics:

  • Simple syntax, small instruction set, fast compilation speed, and high operating efficiency
  • Supports pointers and memory management, can access and modify memory addresses and registers
  • Supports assembly language embedding and calling, enabling interaction with hardware
  • Supports a variety of data types and structures to represent and process complex data
  • Supports static compilation and linking, can generate executable binary files, no runtime environment is required

Representatives of system-level languages ​​include C, C++, Rust, Go, etc. They are widely used in the development of operating systems, drivers, embedded systems, game engines and other fields. The advantages of system-level languages ​​are high performance, strong control, and portability, but they also have some disadvantages, such as complex syntax, difficulty in debugging, and difficulty in ensuring memory safety.

WebAssembly

WebAssembly is an open web standard developed by the W3C WebAssembly Community Group and supported by major browser vendors. Currently, WebAssembly has been adopted by some front-end frameworks and tools, such as Next.js, Astro, SvelteKit, Blazor, Emscripten, etc.

  • A low-level assembly-like language that runs in modern web browsers, has a compact binary format, runs at near-native performance, and provides a compilation target for languages ​​like C, C++, and Rust so that they can run on the web run. It is also designed to coexist with JavaScript, allowing the two to work together.

  • Fast, efficient, portable and safe It can solve some JavaScript performance problems, especially for applications in 3D games, virtual reality, augmented reality, computer vision, image/video editing and other fields. WebAssembly modules can be imported into a web application and expose functions for use by JavaScript. JavaScript can also import functions into WebAssembly modules to achieve two-way interoperability.

Guess you like

Origin blog.csdn.net/olderandyanger/article/details/134879973