5 ms·
When it comes to optimising a Javascript app that involves inserting hundreds and potentially thousands of elements into a page, save yourself headaches and use
by DigitalSea 13y ago
When it comes to optimising a Javascript app that involves inserting hundreds and potentially thousands of elements into a page, save yourself headaches and use documentFragment to prevent reflow and at the same time, insert a lot of elements into your page/app very quickly. I recently build an app that has reached almost 54,000 mouse click records. Each time you click, it saves that value in a database: http://coolcoolcoolcoolcool.com/ http://coolcoolcoolcoolcool.com/
Inspecting the page will yield a click map where I am displaying all 53,000 mouse clicks inside of a map. As you can see I am using canvas now, but before that I was inserting them into the page and believe it or not, using documentFragment it was amazingly fast.
The reason I switched to canvas because performance on mobile was poor. 53,000+ elements rendered by Javascript on desktop didn't cause any fuss though, using documentFragment makes all the difference. Keep in mind, documentFragment really only shines when you are inserting 1000 plus elements into the page at once. One reflow against many reflows is going to cause you a lot of headaches.
- NKCSS 13y agoYour link gives a 502
- DigitalSea 13y agoHaha yeah, it received 12,000 clicks in the space of about 1 hour or so which took it down. Node is good, but it couldn't handle that many clicks.
- mnutt 13y agoIsn't that about 3 clicks a second? Was there a database issue?
- senorprogrammer 13y agoOn average. I suspect his clicks were bursty.
- DigitalSea 13y agoIt seemed the server at times would get hammered and there would be a kind of race condition happening causing a variable for the clicks I am incrementing to have a value of undefined at times (no idea). I was using nodemon instead of forever.js, so when the error would occur, it wouldn't restart. It's all good now.
- thejosh 13y agoSorry dude, I think I just broke your app with a single line of JS.. setInterval(function() {jQuery("#theimage").click()},1);
- recursive 13y agoYou can break any web app with a single line of javascript.