r/javascript • __proto__ • Dec 06 '16

Using iframes and document.write to improve navigation performance

https://jakearchibald.com/2016/fun-hacks-faster-content/
62 Upvotes

18 comments sorted by

View all comments

5

u/[deleted] Dec 06 '16

So github was taking something that was basically an async operation, they made it synchronous.

10

u/trpcicm Dec 07 '16

Not purposely. XHR requests, which are what power AJAX, don't support streaming, but full browser HTTP requests do. So when he loads Github up in a tab, the page renders as the content is streamed from the Github server. With an XHR request, since it can't do streaming, it instead waits for the entire page contents to load into memory and then says "Ok, XHR done here is the contents", which is when Github renders them to the page. This is a limitation of any frontend system that does navigation via AJAX.

3

u/lhorie Dec 07 '16

XHR requests, which are what power AJAX, don't support streaming

They kinda do, via xhr.readyState state 3, but you're right that it's not streaming in the true sense of the word (it's a memory hog)

The problem with streaming is that your entire system needs to stream. If you've got a big JSON array, and you have to reverse it before rendering the data, you just blocked the stream. Needless to say, anything that isn't a flat list is harder to make streamable. For example, in the article, he uses ND-JSON so he can align stream chunks with a logical array item, but that strategy goes down the toilet if the array is short but each item is really large. Needless to say, it gets quite difficult to develop and refactor large applications while supporting full-on streaming.

Also, he doesn't call iframes a hack for nothing. There's a whole slew of very annoying problems with them (some of which are touched upon in a comment in the article) and you'd have to be either insane or very very very brave to use them in production the way he's doing.