React.JS vs Vanilla JS, performance: (28x faster)

Viewed 1825

I'm starting to develop with React, I went through React Native first, so I'm a bit used to their ideas.

What's my problem with these structures:? Performance, speed. I wonder since then.

An example ? I realized when presented some mobile animations, they performed horrible when used in synchronous modes. Maybe I didn't know the framework in an advanced way to understand it and extract its potential, but as you know, using it leaves you in the dark in some parts.

I'll leave here an example of how I reproduced this difference in speed between 'Vanilla JS' and React.JS.

React.JS

I'm just using an API to get all countries (250) and display them after the call.

This is the code: Main-> Container-> List-> Country,

I also used Styled Components, FontAwasome.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './api.js'
import styled from "styled-components";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faUser } from "@fortawesome/free-solid-svg-icons";

class Country extends React.Component {

  clickChild = () => {
    this.props.clicked(this.props.name)
  }

  render() {  
      return (  
        <Box onClick={this.clickChild} className="content">
          <ImageBox flag={this.props.flag}>
            <Icon num={this.props.num}><FontAwesomeIcon icon="user"/></Icon>
          </ImageBox>
        </Box>  
        
      );

  }
}

  class Lista extends React.Component {

    constructor(props) {
        super(props)
        this.state = {lista:[]}
    }

    componentDidMount(){
      let lista_temp = []
      console.log("Iniciando requisição !")
      fetch("https://restcountries.eu/rest/v2/all")
        .then(resp => resp.json())
        .then((json_data)=>{
          for(var i=0;i<json_data.length;i++){
            var population = (json_data[i].population).toLocaleString(undefined,{minimumFractionDigits: 0})
            lista_temp.push({flag:json_data[i].flag,name:json_data[i].translations.br,num:population})
          }
            this.setState({lista:lista_temp})
            console.log("Concluído !")
        });
    }

    click(name){
      window.open('https://pt.wikipedia.org/wiki/'+name,"_blank")
    }

    render() {  
        return (
            <div className="lista">
              {this.state.lista.map((tile,index) => (
                <Country key={index} flag={tile.flag} name={tile.name} num={tile.num} clicked={this.click}/>
              ))}
          </div>            
            
        );

    }
  }



  class Container extends React.Component {



    render() {  
        return (
            <div className="container">
              <div className="main_contant">
                <Lista/>
              </div>
            </div>
        );

    }
  }

  class Main extends React.Component {

    render() {
      return (
            <Container/>
      );
    }
  }
  
  // ========================================
  
  ReactDOM.render(
    <Main />,
    document.getElementById('root')
  );

When we look at the List component, do we have the use of Hooks? I can't say exactly what happens with this use, it seems to be the cause of the problems. Hooks are theoretically not supported in class components.

When we look at its usage, notice that I refresh the list after the entire API is complete, but I can make it much worse by calling setState on each loop item.

this.setState ({list: [... this.state.lista, {flag: json_data [i] .flag, name: json_data [i] .translations.br, num: population}]})

Here are the comparisons in terms of numbers.

A note: I won't put the Vanilla code (I'll leave a link if you like), but I'll show you its interface. It has a lot more interaction than React. In React there is just one click of the event. In vanilla we have animations, autocomplete, consumption of another API and other events. And basically it's an '.append' html for each item consumed by the API.

I don't know why, but I changed the code to post here, and I realized there was an improvement, I don't understand, before it was between 6s - 9s, now it's 4s.

Vanilla

enter image description here

React and broken instruction;

enter image description hereenter image description here

What's wrong with my learning from this framework, any direction?

enter image description here enter image description here

https://github.com/ricardosc12/stack_ask

0 Answers
Related