Top Related Projects
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Deliver web apps with confidence 🚀
web development for the rest of us
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
A rugged, minimal framework for composing JavaScript behavior in your markup.
Quick Overview
Mithril.js is a modern, lightweight JavaScript framework for building single-page applications. It emphasizes simplicity, performance, and a small footprint, making it an excellent choice for developers who want a powerful yet minimalistic tool for creating interactive web applications.
Pros
- Extremely small bundle size (< 10kb gzipped), leading to fast load times
- High performance due to efficient virtual DOM diffing algorithm
- Simple and intuitive API, easy to learn for developers familiar with JavaScript
- Built-in routing and XHR utilities, reducing the need for additional dependencies
Cons
- Smaller community compared to more popular frameworks like React or Vue
- Less extensive ecosystem of third-party components and plugins
- Limited official documentation and learning resources
- May require more manual optimization for complex applications
Code Examples
- Creating a simple component:
const Hello = {
view: () => m("h1", "Hello, World!")
}
m.mount(document.body, Hello)
- Handling user input:
const Counter = {
count: 0,
view: (vnode) => [
m("p", `Count: ${vnode.state.count}`),
m("button", { onclick: () => vnode.state.count++ }, "Increment")
]
}
m.mount(document.body, Counter)
- Making an API request:
const UserList = {
users: [],
oninit: (vnode) => {
m.request({
method: "GET",
url: "https://api.example.com/users"
})
.then(result => {
vnode.state.users = result
})
},
view: (vnode) => m("ul", vnode.state.users.map(user =>
m("li", user.name)
))
}
m.mount(document.body, UserList)
Getting Started
-
Install Mithril.js using npm:
npm install mithril
-
Create an HTML file with a root element:
<!DOCTYPE html> <html> <body> <div id="app"></div> <script src="app.js"></script> </body> </html>
-
Create an
app.js
file with your Mithril application:import m from 'mithril' const App = { view: () => m("h1", "Hello, Mithril!") } m.mount(document.getElementById("app"), App)
-
Bundle your application using a tool like webpack or Rollup, or use a CDN for quick prototyping:
<script src="https://unpkg.com/mithril/mithril.js"></script>
Competitor Comparisons
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
Pros of Vue
- Larger ecosystem and community support
- More comprehensive documentation and learning resources
- Built-in state management solution (Vuex)
Cons of Vue
- Larger bundle size and potentially slower performance
- Steeper learning curve for beginners
- More complex setup and configuration
Code Comparison
Vue:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue!'
}
}
}
</script>
Mithril:
const Hello = {
view: () => m("div", "Hello, Mithril!")
}
m.mount(document.body, Hello)
Vue offers a more template-based approach with separate sections for HTML and JavaScript, while Mithril uses a more concise, JavaScript-centric syntax. Vue's structure may be more familiar to developers coming from traditional web development backgrounds, while Mithril's approach can lead to more compact and potentially faster code.
Both frameworks have their strengths, with Vue excelling in larger applications with complex state management needs, and Mithril shining in smaller, performance-critical projects. The choice between them often depends on specific project requirements and developer preferences.
The library for web and native user interfaces.
Pros of React
- Larger ecosystem and community support
- More extensive documentation and learning resources
- Better performance for complex, large-scale applications
Cons of React
- Steeper learning curve, especially for beginners
- Requires additional libraries for routing and state management
- Larger bundle size, potentially impacting initial load times
Code Comparison
React:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(element, document.getElementById('root'));
Mithril:
var Hello = {
view: function(vnode) {
return m("h1", "Hello, " + vnode.attrs.name);
}
}
m.mount(document.body, {
view: function() {
return m(Hello, {name: "Sara"});
}
});
Both frameworks use a component-based architecture, but React uses JSX syntax while Mithril uses a more JavaScript-centric approach. React's syntax may be more familiar to developers coming from HTML backgrounds, while Mithril's approach might appeal to those who prefer pure JavaScript.
React's larger ecosystem and extensive documentation make it a popular choice for complex applications, but Mithril's simplicity and smaller footprint can be advantageous for lighter projects or when performance is a critical factor.
Deliver web apps with confidence 🚀
Pros of Angular
- Comprehensive framework with built-in features like dependency injection and routing
- Large ecosystem and community support
- Powerful CLI for project scaffolding and development
Cons of Angular
- Steeper learning curve due to its complexity
- Larger bundle size, potentially impacting initial load times
- More opinionated, which can limit flexibility in some cases
Code Comparison
Angular component:
@Component({
selector: 'app-hello',
template: '<h1>Hello, {{name}}!</h1>'
})
export class HelloComponent {
name: string = 'World';
}
Mithril component:
const Hello = {
view: (vnode) => m('h1', `Hello, ${vnode.attrs.name}!`)
};
m(Hello, { name: 'World' });
Key Differences
- Angular uses TypeScript by default, while Mithril is plain JavaScript
- Angular has a more complex setup with decorators and modules
- Mithril's syntax is more concise and closer to vanilla JavaScript
- Angular provides a full-featured framework, while Mithril is a lightweight library
Use Cases
- Angular: Large-scale enterprise applications with complex requirements
- Mithril: Smaller projects or applications prioritizing performance and simplicity
Both frameworks have their strengths, and the choice between them depends on project requirements, team expertise, and performance considerations.
web development for the rest of us
Pros of Svelte
- Compile-time framework, resulting in smaller bundle sizes and better performance
- Simpler syntax with less boilerplate code
- Built-in state management and reactivity
Cons of Svelte
- Smaller ecosystem and community compared to more established frameworks
- Limited use in large-scale production applications
- Steeper learning curve for developers familiar with traditional frameworks
Code Comparison
Svelte component:
<script>
let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>
Clicks: {count}
</button>
Mithril component:
const Counter = {
view: (vnode) => {
let count = vnode.state.count || 0;
return m("button", {
onclick: () => vnode.state.count = count + 1
}, `Clicks: ${count}`);
}
};
Both Svelte and Mithril aim to simplify web development, but they take different approaches. Svelte's compile-time optimizations and intuitive syntax make it attractive for developers seeking performance and ease of use. However, Mithril's lightweight nature and flexibility may be preferred by those who value simplicity and control over their application structure. The choice between the two often depends on project requirements and developer preferences.
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Pros of Preact
- Smaller bundle size (3KB gzipped) compared to Mithril's 10KB
- Closer API compatibility with React, making migration easier
- Faster rendering performance in many scenarios
Cons of Preact
- Less built-in functionality; requires additional libraries for routing and state management
- Smaller community and ecosystem compared to Mithril
- Less opinionated, which may lead to inconsistent code patterns across projects
Code Comparison
Mithril:
m("div", {class: "example"}, [
m("h1", "Hello"),
m("p", "World")
])
Preact:
h("div", {class: "example"}, [
h("h1", null, "Hello"),
h("p", null, "World")
])
Both libraries use a similar virtual DOM approach, but Preact's syntax is closer to React's JSX when used with a transpiler:
<div class="example">
<h1>Hello</h1>
<p>World</p>
</div>
Mithril provides more built-in features like routing and XHR, while Preact focuses on being a lightweight alternative to React. Preact's smaller size and React-like API make it attractive for projects transitioning from React or aiming for minimal bundle size. Mithril's integrated functionality and more opinionated structure can lead to faster development for some projects.
A rugged, minimal framework for composing JavaScript behavior in your markup.
Pros of Alpine
- Lightweight and minimal, with a smaller learning curve
- Easy integration into existing projects without a build step
- Declarative syntax that's intuitive for designers and developers
Cons of Alpine
- Limited ecosystem and fewer advanced features compared to Mithril
- Performance may not be as optimized for large-scale applications
- Less suitable for complex state management scenarios
Code Comparison
Alpine:
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<span x-show="open">Content</span>
</div>
Mithril:
const Toggle = {
view: ({ state }) => [
m("button", { onclick: () => state.open = !state.open }, "Toggle"),
state.open && m("span", "Content")
]
}
m.mount(document.body, Toggle)
Alpine focuses on enhancing HTML with directives, making it easy to add interactivity to existing markup. Mithril, on the other hand, uses a more programmatic approach with virtual DOM and components, offering greater flexibility for complex applications. While Alpine excels in simplicity and quick integration, Mithril provides a more robust foundation for larger projects with its full-featured framework capabilities.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Mithril.js
What is Mithril.js?
A modern client-side JavaScript framework for building Single Page Applications. It's small (8.92 KB gzipped), fast and provides routing and XHR utilities out of the box.
Mithril.js is used by companies like Vimeo and Nike, and open source platforms like Lichess ð.
Mithril.js supports IE11, Firefox ESR, and the last two versions of Firefox, Edge, Safari, and Chrome. No polyfills required. ð
Installation
CDN
<!-- Development: whichever you prefer -->
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mithril/mithril.js"></script>
<!-- Production: whichever you prefer -->
<script src="https://unpkg.com/mithril/mithril.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mithril/mithril.min.js"></script>
npm
npm install mithril --save
The "Getting started" guide is a good place to start learning how to use Mithril.js.
TypeScript type definitions are available from DefinitelyTyped. They can be installed with:
$ npm install @types/mithril --save-dev
Documentation
Documentation lives on mithril.js.org.
You may be interested in the API Docs, a Simple Application, or perhaps some Examples.
Getting Help
Mithril.js has an active & welcoming community on Zulip, or feel free to ask questions on Stack Overflow using the mithril.js
tag.
Contributing
There's a Contributing FAQ on the Mithril.js site that hopefully helps, but if not definitely hop into the Zulip stream and ask away!
Thanks for reading!
ð
Top Related Projects
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Deliver web apps with confidence 🚀
web development for the rest of us
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
A rugged, minimal framework for composing JavaScript behavior in your markup.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot