Hacker News new | past | comments | ask | show | jobs | submit login

Not necessarily addressing the speed portion, but if you're going to give a key to a repeated item in React, it is best to use the index instead of a unique identifier(if the overall DOM structure does not change much between renders) so that React does not destroy and recreate each item on tree change.



funkiee, good catch on that issue. I just tried using the current index during iteration, instead of the actual unique ID of the search results item and that sped things up a lot. In fact, now React and Marko perform almost exactly on par for client-side rendering. Server-side rendering of React components is still very slow in comparison. I will update the README with the new results.

On a related note, the React docs were very misleading when they stated the following:

"In practice, finding a key is not really hard. Most of the time, the element you are going to display already has a unique id. When that's not the case, you can add a new ID property to your model or hash some parts of the content to generate a key." Source: http://facebook.github.io/react/docs/reconciliation.html#key...

I tried following their advice and used the unique ID of the search results record and it turns out that was the wrong thing to do. It was better to use the index of the current iteration and I think that should always be the recommended thing to do given the performance difference.


The next sentence in your quote has some importance too, "Remember that the key only has to be unique among its siblings, not globally unique."

I agree there could be a little more clarity in that block of text, but I think the trade-offs section describes it well. Additionally, if there are any nodes in the tree that have a chance of being the same item between renders, you're going to benefit immensely from implementing a `shouldComponentUpdate` in your `SearchResultsItem.jsx` file.

React certainly doesn't win on every performance level, and if you try to compete on performance you're eventually going to lose. A careful developer could implement this list with no framework at all and have performance that beats both.

In the end, it's more about the mental model that best fits you and your team. I find the composite UI of React where I am building every component of other components in Javascript without a templating language to get in my way to be best suited to me.

If Marko works well for your team and you find it easy to get new developers on board and contributing code then that's awesome, keep up the good work.


funkiee, would you be interested in opening a Github issue or submitting a Pull Request if you believe that is the right thing to do? I'll give it a try in the meantime, but it would be great to have a public discussion on that so others don't make the same mistake (if it does indeed help). Thanks!


It re-creates items even when they have unique identifiers? Is that a bug?


It's as intended. If the ID in a list of 100 changes, and that ID is the key, React is going to assume the tree is different during reconciliation. If you were to use an index, like 0, on pagination the key is still 0 and as such the DOM nodes will be reused. http://facebook.github.io/react/docs/reconciliation.html See #2 in the Trade-offs section.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: