3 ms·
JavaScript is fast. The browser is fast. But communication between the browser and javascript is really really slow. They are written in languages with incom
by admax88q 6y ago
JavaScript is fast. The browser is fast. But communication between the browser and javascript is really really slow.
They are written in languages with incompatible memory models, so lots of data must be copied when communication. They are running in different runtimes, so your javascript JIt can not inline function calls into the DOM.
That's why to this day, if you want to render a bunch of html from javascript, it is faster to generate a giant strin g of markup and pass that to the browser in a single 'innerHTML = "foo"' and let the browser parse all that, than it is to call a bunch of "createElement(); setAttribute(); appendChild();" calls.
- zemnmez 6y agoI benchmarked this recently and I am pretty sure this is not true. Many of these 'single page site frameworks' work via this method.
- admax88q 6y agoYou are correct, I just did my own benchmark, and the DOM approach is no longer correct. It used to be the case a number of years ago when I last benchmarked.
- tiltrus 6y agoCan you share any benchmarks indicating this?
- admax88q 6y agoI whipped up one here with a random comment dump from HN. It appears I'm no longer correct. https://gist.github.com/adamvy/afcace8cbdbe56995626f59f6ea2b9f2 https://gist.github.com/adamvy/afcace8cbdbe56995626f59f6ea2b... Load this as a script tag in an html file to run. Last time I benchmarked this it was true, but that was a number of years ago.
- admax88q 6y agoActually, fun fact, if you remove the clearing of the body, the HTML approach is faster. At least in firefox here. Benchmarking is difficult.