Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips on optimizing javascript

The things I'm trying to get my website to do are getting fairly complex, some of the things that need to be done take long enough that it gives visible delays.

Mainly, adding content to the document slows things down as well as jquery animations that I'm using.

I create elements using document.createElement("div") rather than just doing document.write("<div id='someId'></div>"), and the animations are the custom animation using .animation();

I'm wondering how I can continue to add content as I am but prevent the browser from freezing every time I want to add something.

Any suggestions for speeding things up so there is less of a delay? Tips on what to avoid in javascript programming that gives increased delay would be really helpful.


2 Answers

This sounds more like you want to improve the DOM interaction performance rather than javascript, so in that vein:

  • Yes, document.write is bad, it blocks additional loading (any JS executing before your pages has finished loading basically requires all other processing to stop -- modern browsers like Safari (and by proxy Chrome) and Firefox do a degree of content preloading in order to prevents loads from blocking but subsequent style resolution, etc is largely blocked.
  • document.createElement is in general the best solution, although their are certain cases where just manipulating innerHTML on an element may be faster -- but that is not yet cross browser compatible (i think innerHTML doesn't exist till Firefox 3.5) and the perform characteristics are tricky.
  • Reduce the amount of content you load initially -- eg. if you have lots of content (or content that requires large scripts) attempt to delay loading until after the initial page load has completed.
  • Oh, for animations you should look at CSS animations and they perform much better than any JS implementations, but they're only present in Safari (and by proxy Chrome) and Firefox 3.5 -- definitely not in IE :-(

In terms of JavaScript performance, avoid with and getters/setters like the plague and you should be fine in most modern JS implementations.

like image 181
olliej Avatar answered Sep 04 '26 19:09

olliej


I know this is a couple of years late but better late than never considering this question popped up at the top of google's list for javascript animation optimization.

If you need to add a large amount of elements to the DOM and don't want to use innerHTML to do so you can speed that up I believe by adding the elements to a documentFragment first and then injecting the documentFragment all at once into the DOM. I remember reading that DOM injection is the slowest part of using the standard API to create and add elements. documentFragment allows you to inject all of its contents into the DOM in one go eliminating a large amount of overhead caused by calling appendChild or other insertion methods multiple times. appendChild and other insertion methods supposedly don't suffer the same costs when inserting into a documentFragment opposed to inserting into the DOM. I wish I could find the article I read this in as it had some really nice benchmark tests and explanations. I'm sure you can find more information by doing some searches.

As always though which method to use should be determined by testing on a case by case basis. Eventually you'd probably learn which one to use based on structure of the elements, amount, and use.

PS. There may be an issue with styles and documentFragment but I can't remember what was said about them, either way it'd be something worth checking out.

like image 27
Rich Walrath Avatar answered Sep 04 '26 20:09

Rich Walrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!