Vaishnav Manoj
DataX Journal
Published in
8 min readMar 31, 2023

--

If you’re a web developer, you’re probably familiar with the endless stream of new JavaScript frameworks that seem to pop up every week. One minute it’s Angular, the next it’s Vue, and then React is back in style again. Trying to keep up with the latest trends in web frameworks can feel like an endless game of whack-a-mole, leaving you feeling frustrated and overwhelmed.

Well, fear not, because there’s a new kid on the block that’s worth paying attention to: Svelte. Yes, I know what you’re thinking — “Not another new framework!” But trust me, Svelte is different. It’s a revolutionary approach to building web applications that’s both simple and powerful. In fact, Svelte is so efficient that it may even make you wonder why you bothered with those other frameworks in the first place.

Introduction: The never-ending stream of JavaScript frameworks

Web development can be complex, with a multitude of frameworks to choose from. React is a popular framework known for its virtual DOM and modular design, but it can have a steep learning curve and require a lot of boilerplate code. That’s where Svelte comes in — a new, innovative approach that simplifies development by compiling your code at build time for faster, more efficient code that’s easier to maintain and debug.

In this blog post, I’ll introduce you to Svelte and show you why it’s the framework you’ve been looking for. Whether you’re a seasoned web developer or just starting out, you’re sure to find something to love about Svelte. So sit back, relax, and let’s dive into the world of Svelte!

What is Svelte, and why should you care?

At its core, Svelte is a component-based framework for building user interfaces. What sets Svelte apart is its approach to rendering — unlike other frameworks that rely on a virtual DOM to manage updates to the UI, Svelte compiles your code at build time. This means that your application doesn’t have to do any heavy lifting during runtime, resulting in faster, more efficient code.

So why should you care? Well, for starters, Svelte is incredibly easy to use. Its syntax is straightforward and intuitive, which means you can start building components and applications with minimal ramp-up time. Additionally, because Svelte handles most of the work at compile time, you can focus on building great user experiences instead of worrying about performance and optimization.

But that’s just the beginning. Svelte also offers a number of other benefits, including a small footprint, easy integration with other libraries, and a vibrant community of developers who are passionate about making web development easier and more accessible.

The advantages of using Svelte

So what makes Svelte such an attractive alternative to React? Here are some of the key advantages of using Svelte for your next web development project:

Write Less Code

Svelte is designed to help you write less code. With Svelte, you can build boilerplate-free components using the languages you already know — HTML, CSS, and JavaScript. This makes it easy to build great user experiences without getting bogged down in unnecessary boilerplate code.

Take the example of adding two numbers in Svelte:

<script>
let a = 1;
let b = 2;
</script>

<input type="number" bind:value={a}>
<input type="number" bind:value={b}>

<p>{a} + {b} = {a + b}</p>

In Svelte, all you need to do is define two variables (let a = 1; let b = 2;), bind them to input fields, and display the sum in a paragraph tag ({a} + {b} = {a + b}).

Let’s try to do the same thing in React:

import React, { useState } from 'react';

export default () => {
const [a, setA] = useState(1);
const [b, setB] = useState(2);

function handleChangeA(event) {
setA(+event.target.value);
}

function handleChangeB(event) {
setB(+event.target.value);
}

return (
<div>
<input type="number" value={a} onChange={handleChangeA}/>
<input type="number" value={b} onChange={handleChangeB}/>

<p>{a} + {b} = {a + b}</p>
</div>
);
};

On the other hand, in React, you need to define state variables (useState), write event handler functions (handleChangeA and handleChangeB), bind the input fields to the state variables, and display the sum in a div tag.

As you can see, Svelte allows you to achieve the same result with much less code, making it quicker and easier to develop applications.

No Virtual DOM

In React, the virtual DOM is a lightweight, abstract copy of the actual DOM that is used to optimize rendering performance. React compares the virtual DOM to the previous version and calculates the minimum changes required to update the real DOM accordingly, resulting in “““faster ”””and more “““efficient ””” updates to the UI.

Unlike React, Svelte doesn’t rely on a virtual DOM to manage updates to the user interface. Instead, Svelte compiles your code to tiny, framework-less vanilla JS. This means that your app starts fast and stays fast, without any of the overhead that can slow down your application’s performance.

Truly Reactive

With Svelte, you don’t need to use complex state management libraries. Svelte brings reactivity to JavaScript itself, making it easy to build complex applications without getting bogged down in state management. This makes it easier to focus on building great user experiences, rather than worrying about the underlying implementation details.

Svelte Stores

In Svelte, the equivalent of React’s state management is called “stores”. Svelte stores provide a better way to manage and share data across components compared to React’s state management. Svelte stores are reactive, meaning that any change to the store will automatically trigger a re-render of any component that uses that store. This eliminates the need for complex and error-prone state management code.

Svelte stores are also more flexible than React’s state management. They can be easily shared across components, making it easy to pass data between different parts of your application. Additionally, Svelte stores can be created from any data source, including APIs or databases, making it easy to integrate with external services.

Furthermore, Svelte’s stores are more performant than React’s state management. Svelte stores have a smaller memory footprint and are optimized for performance, which means that your application will be faster and more efficient.

An example of using Svelte Stores:

//Store.js

import { writable } from 'svelte/store';

const UsersStore = writable([
{
name: 'vaishnav',
website: 'vaishnav.tech',
gender: 'male',
twitter: 'vaishnav_mk1',
},
]);

export default UsersStore;
//Users.svelte

import UsersStore from '../Store.js';

let users;
UsersStore.subscribe((data) => {
users = data;
});

Overall, Svelte stores provide a simpler, more flexible, and more performant way to manage data in your application compared to React’s state management.

Insanely Small Bundle Size

One of the biggest advantages of Svelte is its small bundle size. Unlike React, which requires the inclusion of a large library and other dependencies to work, Svelte compiles your code to small, optimized JavaScript files that are easy to include in your project.

Svelte’s approach to compiling your code means that there is no runtime overhead or additional JavaScript libraries required to run your application. This leads to faster load times and a smoother user experience overall.

In addition, Svelte’s compiler analyzes your code and removes any unused components, resulting in even smaller bundle sizes. This means that your users are not forced to download unnecessary code, making your application more efficient and faster to load.

Overall, Svelte’s small bundle size is a major advantage for developers who want to build fast, lightweight web applications.

Comparison from 2019

Whether you’re building a standalone web application or part of a larger ecosystem, Svelte makes it easier to build great user experiences with less code and fewer headaches.

Learning Curve

Compared to React, Svelte has a relatively shallow learning curve. This is partly due to the fact that Svelte’s syntax is more straightforward and closer to traditional HTML, CSS, and JavaScript. In Svelte, you don’t have to worry about learning complex concepts such as JSX or the virtual DOM. Svelte also has a smaller API surface area than React, which makes it easier to get started and build projects quickly.

Additionally, Svelte’s documentation is well-organized and user-friendly, with plenty of examples and explanations. The framework also has a vibrant community that is welcoming and supportive of newcomers. With Svelte, you can get up and running quickly and start building your application without getting bogged down by complex setup or configuration.

So, who is using Svelte?

Svelte has gained popularity among developers and companies in recent years, and it has been adopted by several well-known brands and websites. Some examples of companies using Svelte include The New York Times, Spotify, IBM, and most recently, Apple Music.

These companies have chosen Svelte for its simplicity, ease of use, and ability to create high-performance applications with a small footprint. As more and more companies adopt Svelte, it is becoming an increasingly popular choice for web developers looking for a modern, efficient framework.

Svelte’s Popularity: The Numbers Speak for Themselves

2022 Stackoverflow survey

The Future is Svelte

In conclusion, Svelte is a blazingly fast 🚀, powerful and efficient framework that is quickly gaining popularity in the web development community. With its emphasis on minimalism, simplicity, and fast performance, it’s no surprise that big companies like Apple Music, IBM, and The New York Times have adopted it.

Let’s face it, with the dumpster fire that is useState and useEffect in React, it's time to give Svelte a try. Plus, its growing community and its status as the second most loved framework in Stack Overflow's 2022 survey speak for themselves.

So, if you're a developer who values speed, simplicity, and an intuitive development experience, give Svelte a chance. You won't regret it, and that’s a promise! 💖

Resources to Help You Master Svelte:

Rich Harris’ (creator of Svelte) talk

--

--

Vaishnav Manoj
DataX Journal

Pushing the boundaries of what I know to create weird and wonderful projects!