📜 ⬆️ ⬇️

Comparing JS frameworks: React, Vue and Hyperapp

The author of the material, the translation of which we are publishing today, believes that Hyperapp is a worthy alternative to such web frameworks as React or Vue . He says that the reason for this statement was that he found out that Hyperapp is easier to learn than these two frameworks. His idea was criticized, as it seems that it is based solely on his opinion, and this approach simply does not allow other frameworks to show their strengths. This article is aimed at an objective analysis of Hyperapp, React and Vue, conducted on the basis of simple examples demonstrating their capabilities, and based on the results of their tests.



Example number 1: application counter


Implementing an application counter is probably one of the most frequently used examples in reactive programming. It is extremely simple and straightforward:


Here is an implementation of this example using the frameworks in question.
')

â–ŤReact


 import React from "react"; import ReactDOM from "react-dom"; Class Counter extends React.Component {   constructor(props) {       super(props);       this.state = { count: 0};   }   down(value) {       this.setState(state => ({ count: state.count - value }));   }   up(value) {       this.setState(state => ({ count: state.count + value }));   }   render() {       return (           <div>               <h1>{this.state.count}</h1>               <button onClick = {() => this.down(1)}>-</button>               <button onClick = {() => this.up(1)}>+</button>           </div>       );   } } ReactDOM.render(<Counter />, document.querySelector("#app")); 

â–ŤVue


 import Vue from "vue"; new Vue({   data: { count: 0 },   methods: {       down: function(value) {           this.count -= value;       },       up: function(value) {           this.count += value;       }   },   render: function(h) {       return(           <div>               <h1>{this.count}</h1>               <button onClick={() => this.down(1)}>-</button>               <button onClick={() => this.up(1)}>+</button>           </div>       );   },   el: "#app" }); 

â–ŤHyperapp


 import { h, app } from "hyperapp"; const state = {   count: 0 }; const actions = {   down: value => state => ({ count: state.count - value}),   up: value => state => ({ count: state.count + value}) }; const view = (state, actions) => (   <div>       <h1>{state.count}</h1>       <button onclick={() => actions.down(1)}>-</button>       <button onclick={() => actions.up(1)}>+</button>   </div> ); app(state, actions, view, document.querySelector("#app")); 

â–ŤAnalysis


If you are not familiar with these frameworks, or at least one of them, then you will probably encounter something incomprehensible here. So let's break down this code.


â–Ť Conclusions


If you directly compare the code that solves the same problem, written using all three frameworks, it turns out that Hyperapp, for the implementation of the counter application, requires the least amount of code lines, and this is the only framework that uses the functional approach. However, the amount of code written using Vue, if we count the number of characters, is slightly less, and the use of the element selector in it looks very good. React-application code seems to be the longest, but this does not mean that it is much more difficult to understand than code written to work with other analyzed frameworks.

Example number 2: work with asynchronous code


It is possible that in practice you will have to deal with asynchronous code. One of the most common asynchronous operations is to send a request to a certain API. For the purposes of this example, the JSONPlaceholder API is used , containing conditional data and issuing a list of publications. Here is what we are going to do here:


Consider the code that implements the above scheme of action.

â–ŤReact


 import React from "react"; import ReactDOM from "react-dom"; class PostViewer extends React.Component {   constructor(props) {       super(props);       this.state = { posts: [] };   }   getData() {       fetch(`https://jsonplaceholder.typicode.com/posts`)       .then(response => response.json())       .then(json => {           this.setState(state => ({ posts: json}));       });   }   render() {       return (           <div>               <button onClick={() => this.getData()}>Get posts</button>               {this.state.posts.map(post => (                   <div key={post.id}>                       <h2><font color="#3AC1EF">{post.title}</font></h2>                       <p>{post.body}</p>                   </div>               ))}           </div>       );   } } ReactDOM.render(<PostViewer />, document.querySelector("#app")); 

â–ŤVue


 import Vue from "vue"; new Vue({   data: { posts: [] },   methods: {       getData: function(value) {           fetch(`https://jsonplaceholder.typicode.com/posts`)           .then(response => response.json())           .then(json => {               this.posts = json;           });       }   },   render: function(h) {       return (           <div>               <button onClick={() => this.getData()}>Get posts</button>               {this.posts.map(post => (                   <div key={post.id}>                       <h2><font color="#3AC1EF">{post.title}</font></h2>                       <p>{post.body}</p>                   </div>               ))}           </div>       );   },   el: "#app" }); 

â–ŤHyperapp


 import { h, app } from "hyperapp"; const state = {   posts: [] }; const actions = {   getData: () => (state, actions) => {       fetch(`https://jsonplaceholder.typicode.com/posts`)       .then(response => response.json())       .then(json => {           actions.getDataComplete(json);       });   },   getDataComplete: data => state => ({ posts: data }) }; const view = (state, actions) => (   <div>       <button onclick={() => actions.getData()}>Get posts</button>       {state.posts.map(post => (           <div key={post.id}>               <h2><font color="#3AC1EF">{post.title}</font></h2>               <p>{post.body}</p>           </div>       ))}   </div> ); app(state, actions, view, document.querySelector("#app")); 

â–ŤAnalysis


Let us analyze this code and compare the three frameworks under study.


â–Ť Conclusions


Performing asynchronous operations turned out to be equally simple in all frameworks. Hyperapp can lead a developer to write more functional and modular code when working with asynchronous actions, but the other two frameworks do an excellent job with their task, and, in this regard, give the developer a choice.

Example 3: list item component for a To-Do application


Probably, To-Do-applications are the most famous example in the field of reactive programming. Apparently, something similar is implemented using almost every existing framework. Here we will not implement the entire application. Instead, we will focus on a simple, stateless component in order to explore the capabilities of the frameworks under study to create small building blocks of web applications suitable for reuse.

Consider the implementation of the component with the use of the frameworks under study. In this example, however, we will expand the considered code variants by considering the React-component written in the functional style.

â–ŤReact (functional style)


 function TodoItem(props) {   return (       <li class={props.done ? "done" : ""} onclick={() => props.toggle(props.id)}>           {props.value}       </li>   ); } 

â–ŤReact


 class TodoItem extends React.Component {   render () {       return (           <li class={this.props.done ? "done" : ""} onclick={() => this.props.toggle(this.props.id)}>               {this.props.value}           </li>       );   } } 

â–ŤVue


 var TodoItem = Vue.component("todoitem", {   props: ["id", "value", "done", "toggle"],   template:       '<li v-bind:class="{done : done}" v-on:click="toggle(id)">{{value}}</li>' }); 

â–ŤHyperapp


Pay attention that Hyperapp also uses the functional style.

 const TodoItem = ({ id, value, done, toggle }) = (   <li class={done ? "done" : ""} onclick={() => toggle(id)}>       {value}   </li> ); 

â–ŤAnalysis



â–Ť Conclusions


In order to get used to Vue, it takes some time, as its templates are slightly different from the templates of the other two frameworks. React is extremely flexible, it supports various approaches used to create components. At the same time, everything is very simple in Hyperapp, and it, moreover, is compatible with React, which allows, if necessary, at some stage of the project, to change the framework.

Comparing Component Life Cycle Methods


Another important consideration that influences the choice of the framework is the events of the life cycle of components it supports, which you can subscribe to and which can be processed according to the needs of the developer. Here is a table based on an analysis of the API of the systems under study.
Event
React
Vue
Hyperapp
Initialization
constructor
beforeCreate, created
-
Mounting
comoinentDidMount
beforeMount, mounted
oncreate
Update
componentDidUpdate
beforeUpdate, updated
onupdate
Unmounting
componentWillUnmount
-onremove
Destruction
-beforeDestroy, destroyed
ondestroy

â–ŤAnalysis


Here is what you can understand by analyzing this table:


â–Ť Conclusions


In general, it can be noted that methods for handling events that occur during the life cycle of components support all frameworks. These methods will solve many problems. All three frameworks offer hooks for all kinds of events, but there are minor differences between them, the source of which can be internal features of frameworks and differences in their implementation. Perhaps Vue in this area is one step ahead of other systems, offering a more detailed event handling system, allowing you to handle life cycle events, either before or after they occur.

Framework Performance Comparison


In addition to the ease of use of the framework and the programming techniques used to work with it, many developers are seriously concerned about the performance of frameworks, especially for fairly complex applications. A valuable source of information about the performance of various frameworks is the js-framework-benchmark project.

Therefore, take a look at the test results of React, Vue and Hyperapp.

â–ŤWork with tables


Here are the results of testing frameworks for working with tables. The indicator in the cells of the table corresponds to the duration of the operation ± standard deviation. In parentheses is the result of dividing the resulting indicator by the best indicator.


Analysis



â–ŤDownload, launch, code sizes


Here is a table with the results of a study of indicators related to the launch speed of the framework, its size, and its use of the main thread.


Analysis



â–ŤWorking with memory


Now consider the results of memory allocation tests.


Analysis



â–Ť Conclusions


If, when developing a certain project, you need to achieve maximum performance, you should understand, firstly, what application you are developing, and secondly, clearly identify the needs of this application. If you put together the performance analysis of all three frameworks, you get the feeling that Vue and React are better suited for more complex applications, and Hyperapp shows itself better on smaller-scale applications that need to process less data that requires faster startup. and who may need to work on not the fastest computers.

However, it is worth remembering that the performance tests used here are far from real life, from a certain average scenario. Therefore, having tested and compared them on a real project, you can see other results.

Additional Notes


It should be noted that the comparison of web frameworks may resemble something like the comparison of apples with oranges. Here are some other considerations regarding React, Vue and Hyperapp that may be useful when choosing a specific framework for a certain project:


Results


The author of this material believes that if you have read this far, then you have already formed an understanding of which of the frameworks studied here is best suited to your needs. In the end, we didn’t talk about which of the frameworks is the best, but rather which of them is best able to show itself in various situations. As a result, the following general conclusions can be made:


Dear readers! -?


, - 10% :)

Source: https://habr.com/ru/post/417483/


All Articles