SolidJS: Boost Your Web App Speed

A Quick Guide to SolidJS’s Performance Edge

Configr Technologies
4 min readMay 7, 2024
SolidJS

The right framework is key for building fast, efficient, and user-friendly web applications in JavaScript front-end development.

While React has dominated the scene for years, a rising star is turning heads: SolidJS.

SolidJS promises blazing performance, a React-inspired developer experience, and a unique approach to reactivity.

This quick guide examines SolidJS, exploring its core concepts.

What is SolidJS?

SolidJS is a declarative JavaScript framework for building user interfaces (UIs).

Its primary focus is on delivering exceptional performance and a smooth developer experience.

At its heart, SolidJS leverages the following key principles:

  • Fine-Grained Reactivity: SolidJS tracks changes at a granular level, minimizing unnecessary re-renders and boosting performance.
  • Compilation: Instead of a Virtual DOM like React, SolidJS compiles components into optimized JavaScript code. This leads to smaller bundle sizes and faster initial renders.
  • JSX (JavaScript Syntax Extension): Similar to React, SolidJS employs JSX, offering a familiar way to define UI structure.

Why Consider SolidJS

Let’s look at the compelling reasons to add SolidJS to your web development toolkit:

  • Blazing Speed: SolidJS consistently ranks among the fastest JavaScript frameworks in benchmarks. Its fine-grained reactivity and compilation approach significantly reduce overhead.
  • Developer Experience: If you’re familiar with React, transitioning to SolidJS is remarkably easy. It shares concepts like components, JSX, and a similar mental model.
  • Small Footprint: Compiled SolidJS applications often have remarkably compact bundle sizes, leading to faster loading times.
  • Growing Ecosystem: While its community is smaller than React’s, SolidJS has a vibrant and growing ecosystem with libraries and tooling.

Key Concepts in SolidJS

Let’s break down the essential building blocks of SolidJS development:

Components: As in most modern frameworks, components are the fundamental units of UI structure in SolidJS. They are typically defined as functions that return JSX.

function Counter() {
const [count, setCount] = createSignal(0);

return (
<div>
<button onClick={() => setCount(count() + 1)}>{count()}</button>
</div>
);
}

Reactivity: SolidJS’s reactivity system is based on signals. Signals are simple values that hold state. When a signal’s value changes, SolidJS intelligently updates only the specific parts of the UI that depend on it.

import { createSignal } from 'solid-js';

Effects: Effects allow you to perform side effects in response to reactive value changes. They’re ideal for data fetching, subscriptions, or DOM manipulation.

import { createEffect } from 'solid-js';

createEffect(() => {
console.log('Count changed:', count());
});

Stores: Stores are more complex reactive objects that provide ways to manage state in larger applications. They can hold values, as well as derived data and methods.

import { createStore } from 'solid-js/store';

Building a Sample Application

Let’s build a simple Todo app to illustrate SolidJS concepts.

Setup: You can easily create a SolidJS project using Vite:

npm init vite@latest my-solid-app --template solid
cd my-solid-app
npm install
npm run dev

Todo List Component (App.jsx):

import { createSignal, For } from 'solid-js';
import TodoItem from './TodoItem';

function App() {
const [todos, setTodos] = createSignal<Todo[]>([{ id: 1, text: 'Learn SolidJS', done: false }]);
const [newTodoText, setNewTodoText] = createSignal('');

const addTodo = () => {
setTodos([...todos(), { id: Date.now(), text: newTodoText(), done: false }]);
setNewTodoText('');
};

return (
<div>
<h1>Todo List</h1>
<input type="text" bind:value={newTodoText} onInput={e => setNewTodoText(e.target.value)} />
<button onClick={addTodo}>Add</button>
<ul>
<For each={todos()}>
{(todo) => <TodoItem key={todo.id} todo={todo} />}
</For>
</ul>
</div>
);
}

export default App;

TodoItem Component (TodoItem.jsx):

import { createSignal } from 'solid-js';

interface Todo {
id: number;
text: string;
done: boolean;
}

function TodoItem(props: { todo: Todo }) {
const [done, setDone] = createSignal(props.todo.done);

const toggleDone = () => {
setDone(!done());
};

return (
<li>
<input type="checkbox" checked={done()} onChange={toggleDone} />
<span style={{ textDecoration: done() ? 'line-through' : 'none' }}>{props.todo.text}</span>
</li>
);
}

export default TodoItem;

Explanation:

App.jsx:

  • We manage an array of todos as a signal.
  • The input field lets the user create new Todo items.
  • The addTodo function adds new items to the list.
  • The <For> component from SolidJS lets us iterate and render each todo.

TodoItem.jsx:

  • It takes a todo prop.
  • A checkbox represents the done state.
  • The component’s style changes based on whether a task is complete.

When to Choose SolidJS

SolidJS is a compelling choice if:

  • Performance is paramount: You must squeeze the most performance out of your web application.
  • You love React-like patterns: You want a familiar developer experience.
  • Bundle size matters: You aim for small, lightning-fast initial loads.

Getting Started & Resources

Ready to try SolidJS? Here’s how to get started:

  • Official Documentation: https://www.solidjs.com/
  • SolidJS Awesome: Awesome JS (A curated list of resources)
  • Tutorials and Examples: Find plenty on the SolidJS website and community platforms

Why SolidJS Deserves Your Attention

SolidJS is a powerful player in the ever-changing panorama of JavaScript frameworks.

Its relentless focus on performance, familiar developer experience, and small footprint make it a compelling option for building modern web applications.

If you’re looking for raw speed, developer productivity, and a solution that feels lightweight without sacrificing functionality, SolidJS is worth exploring.

SolidJS

It can disrupt the status quo and become a popular choice for projects where every millisecond counts.

Follow me on Medium, LinkedIn, and Facebook.

Clap my articles if you find them useful, drop comments below, and subscribe to me here on Medium for updates on when I post my latest articles.

Want to help support my future writing endeavors?

You can do any of the above things and/or “Buy me a cup of coffee.

It would be greatly appreciated!

Last and most important, enjoy your Day!

Regards,

George

--

--

Configr Technologies

Technology Insights Updated Multiple Times a Week. If you like what you are reading, you can "buy me a coffee" here: https://paypal.me/configr