2

I've built in the past a "single page application" using nothing besides regular DOM manipulations with JQuery.

Nowadays, I'm rewriting the same app with React-Redux. After re-creating a certain page from my old JQuery app, I've noticed something disturbing: React does the same thing, MUCH MUCH slower. In fact, I'd say it's about twice as slow.

I started optimizing my component, and made sure it renders only when necessary. Yet, the entire process of re-rendering the HTML is slower.

What the component does: i fetch an array of 150 items from the server. Each item might have few items in it. So, my JSX has two nested map() functions:

{this.props.duplicates.length>0 &&(
                   this.props.duplicates.map((duplicate_group,index)=>{
                    return(
                        <form key={index}>
                    {
                        duplicate_group.map((book)=>{
                            return(


                             <div key={book.id}>

                        <label className="specific_product_for_merging" htmlFor={book.id} id={book.id}>
                        <input type="radio" defaultValue={book.id} id={book.id} name="name" />
                         <span className="duplicates_span">Title:<Link to={`/products/${book.id}`}> {book.name}</Link> </span>
                          <span className="duplicates_span">ID: {book.id}</span>
                          <span className=" duplicates_span">Author:<Link to={`/advancedsearch?advancedSearch=byTitle&author=${book.author}`}> {book.author} </Link></span>
                          <span className=" duplicates_span">Publisher:<Link  to={`/advancedsearch?advancedSearch=byTitle&publisher=${book.publisher}`}> {book.publisher}</Link></span>
                          <span  className="duplicates_span">Available Copies: 0</span>
                        </label>
                        <br />


                             </div>


                            ) 
                        })    
                    }

            <input type="submit" className="submit_merged_titles btn btn-primary btn-sm" defaultValue="Merge" />   
        </form>
            )


           })  
         )
          }

I was doing the same exact thing with Jquery. For some reason, the direct DOM manipulation seems to work much faster, in this case at least.

Note that each time i fetch new data(i'm using pagination), all of the data is different, so React has to re-render all DOM elements. But how come the entire process, of re-rendering the virtual DOM, and then rendering the DOM itself, is practically twice as slow?

i.brod
  • 3,993
  • 11
  • 38
  • 74
  • When page changes the DOM structure seems to stay the same in your case. But I don't see how you utilize this - you have a huge component which provides no info to React on how to reuse that virtual DOM, so it has to rebuild it from scratch (it has no AI to understand that your structure doesn't change). I suggest you to create a set of components for rendering, i.e. `root` renderer, `duplicate_group` and `book`. In other words: you should provide a diff to React, not a new batch of content. – user3707125 Jan 06 '18 at 18:07
  • Not too surprising, every JSX element like ` – Andy Ray Jan 06 '18 at 18:13
  • Thank you for your replies. To user3707125: I'll try to create some "informative" component structure, as you suggested. The "duplicate group", is always a form with some submit button, so obviously it doesn't change. To Andy Ray: Well, i would like to know how to make it better :D I think i got the "why" already:-). Besides user3707125's suggestions, is there anything else u can recommend? – i.brod Jan 06 '18 at 19:07

0 Answers0