Sencha Con 2013: Fastbook

I didn’t plan on writing a post purely on Fastbook, but Jacky’s presentation just now was so good I felt it needed one. If you haven’t seen Fastbook yet, it is Sencha’s answer to the (over reported) comments by Zuckerburg that using HTML5 for Facebook’s mobile app was a mistake.

Jacky on stage

After those comments there was a lot of debate around whether HTML5 is ready for the big time. Plenty of opinions were thrown around, but not all based on evidence. Jacky was curious about why Facebook’s old app was so slow, and wondered if he could use the same technologies to achieve a much better result. To say he was successful would be a spectacular understatement – Fastbook absolutely flies.

Performance can be hard to describe in words, so Sencha released this video that demonstrates the HTML5 Fastbook app against the new native Facebook apps. As you can see, not only is the HTML5 version at least as fast and fluid as the native versions, in several cases it’s actually significantly better (especially on Android).

Challenges

The biggest challenge here is dynamically loading and scrolling large quantities of data while presenting a 60fps experience to the user. 60fps means you have just 16.7ms per frame to do everything, which is a hugely tall order on a CPU and memory constrained mobile device.

The way to achieve this is to treat the page as an app rather than a traditional web page. This means we need to be a lot more proactive in managing how and when things are rendered – something that traditionally has been in the domain of the browser’s own rendering and layout engines. Thankfully, the framework will do all of this for you.

As an example, Jacky loaded up Gmail’s web app and showed what happens when you scroll a long way down your inbox. The more you scroll, the more divs are added to the document (one new div per message). Each div contains a bunch of child elements too, so we’re adding maybe a dozen or so nodes to our DOM tree per message.

The problem with this is that as the DOM tree gets larger and larger, everything slows down. You could see the inspector showing slower and slower layout recalculations, making the app sluggish.

The solution is to recycle DOM nodes once they’re no longer visible. In this way, a list that seems to have infinite content could contain only say 10 elements – just enough to fill the screen. Once you scroll down the list, DOM nodes that scrolled off the top are detached, updated with new data and placed at the bottom of the list. Simple. Ingenius. Beautiful.

Prioritization

There’s usually a lot more going on in an app than just animating a scrolling view though. There’s data to load via AJAX, images to load, compositing, processing, and whatever else your app needs to do. And then there are touch events, which need to feel perfectly responsive at all times, even while all of this is going on.

To make this sane and manageable, we have a new class called AnimationQueue. All of the jobs I just mentioned above – handling touch events, animation, network requests and so on – are dispatched through the AnimationQueue with a given priority. Touch event handling has the top priority, followed by animation, followed by everything else.

AnimationQueue does as much as it can in that 16.7ms window, then breaks execution again to allow the browser to reflow/repaint/whatever else it needs to do. What this means is that while scrolling down a large list, it’s likely that our CPU/GPU is being taxed so much that we don’t have any time to load images or other low priority jobs.

This is a Good Thing, because if we’re scrolling through a large list there’s a good chance we are going to skip right over those images anyway. In the end they’re loaded as soon as the AnimationQueue has some spare time, which is normally when your scrolling of the list has slowed down or stopped.

Sandboxing

The final, and most complex technique Jacky discussed was Sandboxing. The larger your application gets, the larger the DOM tree. Even if you are using best practices, there’s an expense to simply having so many components on the same page. The bottleneck here is in the browser itself – looks like we need another hack.

To get around this, we can dynamically create iframes that contain parts of our DOM tree. This way our main page DOM tree can remain small but we can still have a huge application. This not only speeds up browser repaint and reflow, it also improves compositing performance, DOM querying and more.

This all happens under the covers and Jacky’s aiming on including Ext.Sandbox in Sencha Touch 2.3 so that all apps can take advantage of this huge improvement. He cautioned (rightly) that it’ll only make 2.3 if it’s up to his high standards though, so watch this space.

7 Responses to Sencha Con 2013: Fastbook

  1. Pingback: Sencha Con 2013: Ext JS Performance tips | Ed Spencer

  2. Pingback: Sencha Con 2013 Wrapup | Ed Spencer

  3. This sounds amazing and looks like Facebook guys are fools for leaving HTML5 at first, but it’s far from it. Just to show news feeds, Facebook’s own mobile web is quite fast, almost same as demoed here. No difference at all. The Facebook mobile app has to support lot more than that – ability to give permission to other apps, list, install and run apps (device specific), run games/apps inside it’s own app itself, perform sync on various levels, have a payment mechanism, are just a few. In light of those, this is just other Facebook *news feed* fork which is redundant. We must appreciate Facebook’s real effort to go native here.

    • edspencer says:

      Well I think it was more to address the slur on HTML5’s reputation after Facebook referenced it as their greatest mistake with regards to its mobile app. The principal criticism against that app was its terrible performance, so Sencha is just busting the myth that HTML5 = slow.

      Sencha wasn’t saying Facebook should go back to HTML5, it’s just vindicating HTML5 from the blame unfairly sent its way. Clearly, HTML5 as a technology was not the cause of the old app’s performance issues.

      On the capabilities front it’s true that native apps have access to many more APIs. But in-app purchases could easily be presented for an authenticated user with a stored payment method. Sync can be done with or without a native app – just talk JSON over AJAX. We’re even starting to get camera access now, which has some amazing implications for client side real time video processing.

      Like any new technology, we haven’t yet realized the most important features of HTML5 yet. Its capabilities are likely to grow.

  4. gui says:

    Maybe low level question, about “The solution is to recycle DOM nodes once they’re no longer visible”
    How can we know it is invisible while scrolling using Sencha Touch API?

  5. When do you’ll publish the video of this presentation ?

  6. edspencer says:

    @Walter I expect the videos will be released publicly fairly soon, but as I’m not a Sencha employee I don’t have any actual information on that :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,607 other followers

%d bloggers like this: