Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



I benchmarked this recently and I am pretty sure this is not true. Many of these 'single page site frameworks' work via this method.


You 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.


Can you share any benchmarks indicating this?


I whipped up one here with a random comment dump from HN. It appears I'm no longer correct.

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.


Actually, fun fact, if you remove the clearing of the body, the HTML approach is faster.

At least in firefox here.

Benchmarking is difficult.




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

Search: