Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,195,427 members, 7,958,277 topics. Date: Wednesday, 25 September 2024 at 11:46 AM

Mithril.js Vs React Why Mithril.js Is Better - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Mithril.js Vs React Why Mithril.js Is Better (495 Views)

Bun.js: Is It The Future? / Node Js Is Dead ? Long Live Deno Js / Flutter Vs React Native Vs Xamarin For Cross Platform Development (2) (3) (4)

(1) (Reply) (Go Down)

Mithril.js Vs React Why Mithril.js Is Better by chim14(m): 6:46am On Aug 14, 2021
React and Mithril share a lot of similarities. If you already learned React, you already know almost all you need to build apps with Mithril.

https://mithril.js.org/framework-comparison.html

They both use virtual DOM, lifecycle methods and key-based reconciliation
They both organize views via components
They both use JavaScript as a flow control mechanism within views
The most obvious difference between React and Mithril is in their scope. React is a view library, so a typical React-based application relies on third-party libraries for routing, XHR and state management. Using a library oriented approach allows developers to customize their stack to precisely match their needs. The not-so-nice way of saying that is that React-based architectures can vary wildly from project to project, and that those projects are that much more likely to cross the 1MB size line.

Mithril has built-in modules for common necessities such as routing and XHR, and the guide demonstrates idiomatic usage. This approach is preferable for teams that value consistency and ease of onboarding.

Performance
Both React and Mithril care strongly about rendering performance, but go about it in different ways. In the past React had two DOM rendering implementations (one using the DOM API, and one using innerHTML). Its upcoming fiber architecture introduces scheduling and prioritization of units of work. React also has a sophisticated build system that disables various checks and error messages for production deployments, and various browser-specific optimizations. In addition, there are also several performance-oriented libraries that leverage React's shouldComponentUpdate hook and immutable data structure libraries' fast object equality checking properties to reduce virtual DOM reconciliation times. Generally speaking, React's approach to performance is to engineer relatively complex solutions.

Mithril follows the less-is-more school of thought. It has a substantially smaller, aggressively optimized codebase. The rationale is that a small codebase is easier to audit and optimize, and ultimately results in less code being run.

Here's a comparison of library load times, i.e. the time it takes to parse and run the JavaScript code for each framework, by adding a console.time() call on the first line and a console.timeEnd() call on the last of a script that is composed solely of framework code. For your reading convenience, here are best-of-20 results with logging code manually added to bundled scripts, running from the filesystem, in Chrome on a modest 2010 PC desktop:

55.8 ms
4.5 ms
Library load times matter in applications that don't stay open for long periods of time (for example, anything in mobile) and cannot be improved via caching or other optimization techniques.

Since this is a micro-benchmark, you are encouraged to replicate these tests yourself since hardware can heavily affect the numbers. Note that bundler frameworks like Webpack can move dependencies out before the timer calls to emulate static module resolution, so you should either copy the code from the compiled CDN files or open the output file from the bundler library, and manually add the high resolution timer calls console.time and console.timeEnd to the bundled script. Avoid using new Date and performance.now, as those mechanisms are not as statistically accurate.

For your reading convenience, here's a version of that benchmark adapted to use CDNs on the web: the benchmark for React is here, and the benchmark for Mithril is here. Note that we're benchmarking all of Mithril rather than benchmarking only the rendering module (which would be equivalent in scope to React). Also note that this CDN-driven setup incurs some overheads due to fetching resources from disk cache (~2ms per resource). Due to those reasons, the numbers here are not entirely accurate, but they should be sufficient to observe that Mithril's initialization speed is noticeably better than React.

Here's a slightly more meaningful benchmark: measuring the scripting time for creating 10,000 divs (and 10,000 text nodes). Again, here's the benchmark code for React and Mithril. Their best results are shown below:

99.7 ms
42.8 ms
What these numbers show is that not only does Mithril initializes significantly faster, it can process upwards of 20,000 virtual DOM nodes before React is ready to use.

Update performance
Update performance can be even more important than first-render performance, since updates can happen many times while a Single Page Application is running.

A useful tool to benchmark update performance is a tool developed by the Ember team called DbMonster. It updates a table as fast as it can and measures frames per second (FPS) and JavaScript times (min, max and mean). The FPS count can be difficult to evaluate since it also includes browser repaint times and setTimeout clamping delay, so the most meaningful number to look at is the mean render time. You can compare a React implementation and a Mithril implementation. Sample results are shown below:

12.1 ms
6.4 ms
Development performance
Another thing to keep in mind is that because React adds extra checks and helpful error messages in development mode, it is slower in development than the production version used for the benchmarks above. To illustrate, here's the 10,000 node benchmark from above using the development version of React.

Drop-in replacements
There are several projects that claim API parity with React (some via compatibility layer libraries), but they are not fully compatible (e.g. PropType support is usually stubbed out, synthetic events are sometimes not supported, and some APIs have different semantics). Note that these libraries typically also include features of their own that are not part of the official React API, which may become problematic down the road if one decides to switch back to React Fiber.

Claims about small download size (compared to React) are accurate, but most of these libraries are slightly larger than Mithril's renderer module. Preact is the only exception.

Be wary of aggressive performance claims, as benchmarks used by some of these projects are known to be out-of-date and flawed (in the sense that they can be - and are - exploited). Boris Kaul (author of some of the benchmarks) has written in detail about how benchmarks are gamed. Another thing to keep in mind is that some benchmarks aggressively use advanced optimization features and thus demonstrate potential performance, i.e. performance that is possible given some caveats, but realistically unlikely unless you actively spend the time to go over your entire codebase identifying optimization candidates and evaluating the regression risks brought by the optimization caveats.

In the spirit of demonstrating typical performance characteristics, the benchmarks presented in this comparison page are implemented in an apples-to-apples, naive, idiomatic way (i.e. the way you would normally write 99% of your code) and do not employ tricks or advanced optimizations to make one or other framework look artificially better. You are encouraged to contribute a PR if you feel any DbMonster implementation here could be written more idiomatically.

Complexity
Both React and Mithril have relatively small API surfaces compared to other frameworks, which help ease learning curve. However, whereas idiomatic Mithril can be written without loss of readability using plain ES5 and no other dependencies, idiomatic React relies heavily on complex tooling (e.g. Babel, JSX plugin, etc), and this level of complexity frequently extends to popular parts of its ecosystem, be it in the form of syntax extensions (e.g. non-standard object spread syntax in Redux), architectures (e.g. ones using immutable data libraries), or bells and whistles (e.g. hot module reloading).

While complex toolchains are also possible with Mithril and other frameworks alike, it's strongly recommended that you follow the KISS and YAGNI principles when using Mithril.

Learning curve
Both React and Mithril have relatively small learning curves. React's learning curve mostly involves understanding components and their lifecycle. The learning curve for Mithril components is nearly identical. There are obviously more APIs to learn in Mithril, since Mithril also includes routing and XHR, but the learning curve would be fairly similar to learning React, React Router and a XHR library like superagent or axios.

Idiomatic React requires working knowledge of JSX and its caveats, and therefore there's also a small learning curve related to Babel.

Documentation
React documentation is clear and well written, and includes a good API reference, tutorials for getting started, as well as pages covering various advanced concepts. Unfortunately, since React is limited to being only a view library, its documentation does not explore how to use React idiomatically in the context of a real-life application. As a result, there are many popular state management libraries and thus architectures using React can differ drastically from company to company (or even between projects).

Mithril documentation also includes introductory tutorials, pages about advanced concepts, and an extensive API reference section, which includes input/output type information, examples for various common use cases and advice against misuse and anti-patterns. It also includes a cheatsheet for quick reference.

Mithril documentation also demonstrates simple, close-to-the-metal solutions to common use cases in real-life applications where it's appropriate to inform a developer that web standards may be now on par with larger established libraries.
Re: Mithril.js Vs React Why Mithril.js Is Better by niel63(m): 11:13pm On Aug 14, 2021
You people are just speaking, using different vocabulary for a language that has a simple meaning.

I went through your docs and api...
It's okay... but give me a good reason why I should leave a framework that's just a twist off the same framework you're using as comparison. cool

Good job though.
Re: Mithril.js Vs React Why Mithril.js Is Better by bularuz(m): 11:41am On Aug 15, 2021
Mithril appears to be much easier to learn and faster since it has a small size, however a lot of work need to be done if they want to join the big 3 javascript framework putting in mind the other numerous framework like svelte and ember js. Good luck
Re: Mithril.js Vs React Why Mithril.js Is Better by chim14(m): 9:34am On Aug 28, 2021
niel63:
You people are just speaking, using different vocabulary for a language that has a simple meaning.

I went through your docs and api...
It's okay... but give me a good reason why I should leave a framework that's just a twist off the same framework you're using as comparison. cool

Good job though.

Because is simple, easy to use and setup compared to the one you are using and you can still do advanced stuff with mithril.js like you would other frame works
Re: Mithril.js Vs React Why Mithril.js Is Better by chim14(m): 9:37am On Aug 28, 2021
bularuz:
Mithril appears to be much easier to learn and faster since it has a small size, however a lot of work need to be done if they want to join the big 3 javascript framework putting in mind the other numerous framework like svelte and ember js. Good luck

Its not about complexity or whats in vogue, its about simplicity in solving complex stuff. In the IT world, we too like headache... I rather settle for something simple, easy, straightforward and get my job done.
Re: Mithril.js Vs React Why Mithril.js Is Better by bularuz(m): 5:03pm On Sep 15, 2021
chim14:


Its not about complexity or whats in vogue, its about simplicity in solving complex stuff. In the IT world, we too like headache... I rather settle for something simple, easy, straightforward and get my job done.
Mithril.js is far more simple and straightforward, it even comes with it request and routing modules, I wonder why it's not more popular?
Re: Mithril.js Vs React Why Mithril.js Is Better by Heathen(m): 9:52am On Sep 16, 2021
God I hate frontend, there seems to been a new framework or library every other month. Disgusting.
Re: Mithril.js Vs React Why Mithril.js Is Better by chim14(m): 2:31pm On Sep 16, 2021
Heathen:
God I hate frontend, there seems to been a new framework or library every other month. Disgusting.

Thats the problem with wedevelopment. If its desktop app or mobile app like android or ios, you just have only one consistent UI library

(1) (Reply)

Argument Settlement! / Can I Learn Dapp/web3 Development Without Any Knowledge Of Coding Or Programming / I Am Building The Next Unsplash

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.