What is Svelte?

What is Svelte?

Imagine you're building a house.

Traditional Frameworks (like React):

  • The "Builder" in the Browser: React is like a builder that works inside your house, constantly checking for changes and then making small fixes as needed. This builder (the React library) is always there, using up space and resources.

  • Extra Tools: This builder also carries a bunch of extra tools (the Virtual DOM, reconciliation logic, etc.) that it uses to do its job, which takes up even more space and energy.

  • House is slower The builder can slow down the work, especially in big or complex houses.

Svelte Components (especially in Svelte 5):

  • The "Prefab" Approach: Svelte is a tool for building web applications. It's like a factory that prepares all the pieces of your house in advance (your components). These pre-built pieces are very efficient, each purpose-built to a specific job.

  • Declarative Building: Just like other user interface frameworks, it allows you to build your app declaratively out of components; you focus on what you want to achieve, not how. that combine markup, styles, and behaviors. These components are written in a combination of HTML (markup), CSS (styles), and JavaScript (for behavior and reactivity).

            <!-- Basic Svelte Component Example -->
      <script>
          let message = "Hello, Svelte!";
      </script>
    
      <p>{message}</p>
      <button>Change Message</button>
    
      <style>
          p {
              font-size: 18px;
              color: blue;
          }
      </style>
    
  • Factory Output: The factory (Svelte compiler) doesn't just spit out pieces, but it transforms them into instructions that are very simple and fast to follow. They're like "do this, then do that" commands for the browser.

  • No Extra Builder, Just Instructions: The instructions for how to put together and change the pieces are very light and compact. You don't need a large framework to keep monitoring and managing the process in the browser.

  • House is faster: No extra builder makes the work faster, uses less resources, and makes your whole house lighter.

What does "compiled into small, efficient JavaScript modules" mean?

  • Compiled: It means that Svelte takes your component code (HTML, CSS, and JavaScript with runes) and transforms it into pure JavaScript before your website even loads in the browser. It does this heavy lifting ahead of time.

  • Small: Because Svelte does so much work upfront, it means less JavaScript code needs to be sent to the browser. This results in smaller file sizes, meaning your website loads faster.

  • Efficient: The code produced is not just small, it is optimized. There are no extra steps, no extra virtual representations that the browser needs to handle. The code instructions are very fast and streamlined.

  • JavaScript modules: These are just like smaller, more organized javascript files that are easier to handle and combine into a bigger application.

  • These components are compiled into small, efficient JavaScript modules that make your app faster and lighter.

Eliminating Overhead:

The "overhead" that is being removed is all of the extra processes that runtime libraries like React have to perform. This includes:

  • Virtual DOM reconciliation: React has to compare the virtual DOM every time something changes.

  • Keeping track of updates: React needs to manage lots of complex functions.

  • Large Framework Libraries: You have to download a library to run React, which makes your application bigger.

Svelte does not have these, because all of that is done during the compilation process.

In summary:

Svelte is a tool for building web applications. Svelte components are like pre-built, highly optimized pieces for your website. Like other user interface frameworks, it allows you to build your app declaratively out of components that combine markup, styles and behaviors. They're created ahead of time, so they're very efficient and make your site faster. These components are compiled into small, efficient JavaScript modules that eliminate overhead traditionally associated with UI frameworks. Svelte shifts the heavy lifting from the browser (where React does it) to the compiler. Less code, faster speeds, and a more streamlined experience!