Why Svelte is different - and awesome!

Svelte is not just another JavaScript framework. It's a compiler. Read what that means and what the implications are.

Featured on Hashnode

Just to get this out of the way in the beginning:

This is not meant to be bashing other frameworks like React, Vue, or Angular. I used all of them and React (using NextJS) is mostly still my go-to.

What is Svelte?

Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue do the bulk of their work in the browser, Svelte shifts that work into a compile step that happens when you build your app.

TLDR; it's like React or Vue, but with the main difference that it is a compiler.

There is a quote in the blog article linked above:

Wait, this new framework has a runtime? Ugh. Thanks, I'll pass.

ā€“ front end developers in 2018

Even though this didn't happen in 2018, I think we'll get to that mindset at some point.

What does "Svelte is a compiler" mean?

It essentially means that Svelte-specific code gets compiled (think about transformed) to JavaScript, which is executable by the browser.

Another compiler you might know is the TypeScript compiler (tsc), which compiles TypeScript to JavaScript. It's the same concept.

So what is the deal? You can also write React code as .js and ship it. That is true, BUT that JavaScript code does not work in the browser without shipping the React runtime system as well.

A runtime system refers to the collection of software and hardware resources that enable a software program to be executed on a computer system.

Note: Even though I a lot of people are talking about "(no) runtime", it should more accurately be "(no) runtime system".

Read the great React as a UI Runtime blog post from Dan Abramov. It explains React being a runtime (system) in depth.

There is also another benefit besides not require a runtime. Svelte could extend and change the JavaScript syntax because the Compiler compiles the JavaScript in the end. Therefore Svelte could get rid of some limitations provided by JavaScript syntax.

This could also be a downside because if Svelte would strongly deviate from JavaScript syntax, it would essentially become another language to learn. No worries, Svelte tries to stick to the JavaScript Syntax.

Benefits resulting from Svelte being a compiler

Since Svelte is a compiler and therefore does not require a runtime system to be loaded into the client, there are several advantages. These are what make Svelte special. The most important advantages that came to my mind are shown in the next sections.

Performance

This one should be obvious: No runtime to load for the client results in faster load times.

The following image shows an excerpt of a JS framework benchmark (see this GitHub repo). It's based on a large table with randomized entries and measures the time for various operations including rendering duration.

image.png

The app using Svelte ships the lowest amount of code. (Somehow Svelte seems to require less code than vanilla JS, I have no clue how that could happen šŸ˜…)

But it not only ships less code to the client but also executes faster:

image.png

One of the reasons for this is that Svelte does not use a virtual DOM (vDOM). Svelte does not rely on the diff between vDOM and DOM to update the DOM. The other mentioned frameworks like React, Vue and Angular (EDIT: Angular uses Incremental DOM) do use the vDOM. You can read details about this in Svelte's blog post Virtual DOM is pure overhead.

A quick quote from that post:

Svelte is a compiler that knows at build time how things could change in your app, rather than waiting to do the work at run time.

Svelte in a Micro-Frontend Architecture

Micro-Frontends (MFEs) is a topic in itself (read about it in this article proved by Martin Fowler). But the concept is basically that different teams can separately develop distinct parts of the frontend. Teams can also choose the tech stack they want to use. Therefore the client could end up loading different versions of Angular, Vue, React, etc.:

Some micro frontend implementations can lead to duplication of dependencies, increasing the number of bytes our users must download. (from Martin Fowler article linked above)

But what about Svelte? Svelte (also using different versions of it) does not come with the downside of increasing the KBs a client has to load.

Svelte is an awesome choice for MFE Architecture.

Other benefits

These benefits do not result from Svelte being a compiler, but they make Svelte stand out anyways.

REPL

Svelte has an awesome REPL. You can start playing around and try out things without any effort. This is awesome! Try it out.

You can also see the compiled JS and outputted CSS (it can be written in the same .svelte file) by clicking on "JS Output" or "CSS Output" respectively.

Is this evidence enough that Svelte is a compiler? šŸ˜‰

The REPL is used in their awesome tutorial as well. You can learn Svelte hands-on: Svelte tutorial.

Builtin features

Svelte has some features built-in that you need in almost any app anyways (at least larger ones), like transitions, animations, and a store. No need for an additional dependency or the decision between various choices in the first place.

A store is simply an object with a subscribe method that allows interested parties to be notified whenever the store value changes.

import { writable } from 'svelte/store';

export const count = writable(0);

export const increment = () => {
    count.update(n => n + 1);
}

That's it. You can import count and increment across your app. Simple!

Try out Svelte store tutorial

Animations and transitions in Svelte are easy to use. Can you guess what the following code is doing?

{#if visible}
    <p in:fly="{{ y: 200, duration: 2000 }}" out:fade>
        Flies in, fades out
    </p>
{/if}

Try out Svelte transitions tutorial

But they can also be used for more complex things, like the following:

animations.gif

Have fun building this in React šŸ¤Ŗ

Try out Svelte animations tutorial

SvelteKit

SvelteKit is a topic of its own. But it is one of the main reasons why I am so excited. Think of SvelteKit being for Svelte what NextJS is for React.

But why is it awesome?

SvelteKit fully embraces the serverless paradigm, and will launch with support for all the major serverless providers, with an 'adapter' API for targeting any platforms that we don't officially cater to.

Read about it in What's the deal with SvelteKit?

As I am writing this SvelteKit is currently in beta. Can't wait for the release!

Conclusion

I could go on and on and on (have I mentioned Svelte is written in TypeScript?). But this wraps it up. You can see that I am excited, right? Svelte is something I would place my bet on. Learning Svelte and the differences to runtime system-based frameworks is definitely not a waste of time.

I am looking forward to Sveltes evolution in the future. I hope that it will soon be more widely used and I can start client projects using Svelte šŸ˜‰

James Tucker's photo

Really good overview of Svelte, Jannik! Will point others to this post when they ask what's so great about it. :D

Jannik Wempe's photo

Thanks, James. I basically wrote it for the same reason. Now I can point to this myself šŸ˜…

Juan F Gonzalez's photo

Yes indeed. As I have written before, Svelte is a really good way to get into web development for beginners but also for more veterans that want a different, more lightweight and performant way to build stuff with.

Jannik Wempe's photo

That's true. I think the fact that you don't have to decide between various stores or animation libraries etc. makes it easier to get started.

Mayur Borse's photo

Nice Intro.

Favourite Jome's photo

A interesting read about Svelte

Jannik Wempe's photo

Happy you like it šŸ˜Š

Phillip Ninan's photo

Great read my friend!

Jannik Wempe's photo

Hey, thanks for the nice words :)

Tenzin woeser's photo

looks promising

Jannik Wempe's photo

It does! I hope for SvelteKit v1 bringing the liftoff (therefore the rocket reaction šŸ¤Ŗ)

Saquib Hussain's photo

This does look like the next big thing! Do you think it will be?

Show +4 replies
Brandon's photo

James Tucker There are multiple issues filed related to CSS and trying to figure out workarounds, but here's the issue I created github.com/sveltejs/svelte/issues/4764 and here is another related example github.com/sveltejs/rfcs/pull/13

Jannik Wempe's photo

Brandon Sad you had to make these experiences ā˜¹ļø But it can also be seen at one of Sveltes strength: they enforce best practices (and not touching components styles from outside, besides a defined interface for that, is one of them). Only use case I can think of are libraries like Bootstrap etc.

Nevertheless, it is hard (or impossible?) to please everyone. Maybe at some point your time for Svelte will come as well šŸ˜Š

Appriciate your critic perspective!

Don Alfons Nisnoni's photo

Yeah... Svelte is good, i like it and use it a lot for my side projects. But thinks to know is React still dominates frontend framework, most job says react. I can't wait for svelte to be popular on job market. Though svelte is easy to learn than react. So my advice is learn svelte when you have free time. Great introduction man!

Jannik Wempe's photo

100%! I am currently mostly doing client work in react, but I'll transition to Svelte.

In my opinion it is a good opportunity to be part of Sveltes community while it's still rather small.