Results 1 to 7 of 7

Thread: Animation with the DOM and document reflows

  1. #1
    Join Date
    Sep 2006

    Animation with the DOM and document reflows

    I'm doing various animations in JavaScript, and was searching the net for different ways to speed it up/make it smoother/makes it easier on the computer. I stumbled upon this:

    Web browsers weren't really developed with DOM-based animation (that is, using native HTML elements) in mind; repositioning and re-rendering an image or <div> element within any document means that a certain amount of "reflow" has to happen, where the browser must not only re-draw but also do some calculations to determine if and how the repositioned element has affected the flow of nearby elements and the overall document. This logic is also seen in action when fluid sites dynamically rearrange themselves to fit one's browser window when being resized.

    Now, being curious about this, I started making some benchmarking tests, to see if I could avoid these reflows. Some websites talked about having several DOM changes after each other might avoid additional reflows, because the browsers waits to see if several DOM changes happens at once, allowing the browser to apply them all in a single reflow.

    So, I created a big animation thingy, with 200 objects flying around, and I take the time before and after the code has looped though each object, animating it for that "frame" or "loop" or whatever you want to call it. This was going to be my base, onto which I would try different optimizing tricks. It displays the amount of milliseconds between each grapical loop.

    However, I was a bit amazed, because despite those 200 objects flying around, my benchmarking shows that looping though all these objects takes...10 milliseconds.

    Now I was kinda confused. I'm running on Linux, so I don't have IE6 to test with, I did this test in FF3, which might explain it, but still. I'm kinda confused. This performance is so good that I don't really need any optimizing at all!

    Here is my test thus far without any modifications

    So, I started thinking, could it be that the reflow is asynchronous? That would explain why it escapes my benchmarking tests.

    So, here I am. I thought I'd ask you, to see if you know. Is the reflow asynchronous? Has any of you done any tests as to how one can limit the total amount of reflows?

  2. #2
    Join Date
    Sep 2006
    Copenhagen, Denmark
    I too am interested in the reflow issue. If the elements you are moving around is positioned absolute then those element should not effect the document layout...and changes to those should not cause the entire page to reflow.

    I'm not 100&#37; sure on this but it does make sense. If you can verify it or have another explanation I would like to know.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  3. #3
    Join Date
    Sep 2006
    Hmh. That's a good point. Absolute elements shouldn't have any effect upon the document flow, at least in theory. It wouldn't surprise me if Firefox and Opera handles this well, while IE6 and such screws up.

    Anyway, I'm thinking of making a system where I measure the browser strain (and thus asynchronous work too), by having a timeout fire very frequently, and then messsuring how delayed it is (which I have experienced when there are lots of things happening in the browser).

  4. #4
    Join Date
    Sep 2006
    Seeing that several users have showed interest in this (mostly over email, but also Dok), I will post about any information I can find about reflows here.

    What Opera has to say about reflows

    This is very interesting, because Opera describes that there are two different things that can happen, a reflow, and a repaint. A repaint happens when you change something that doesn't affect the layout of the document, like for example changing the background color of an element. It also talks about how absolutely and fixed elements do not force a reflow upon the whole page, but only the object itself (and that probably due to that even an absolutely positioned element can contain child elements that act according to a flow).

    This is very good for us, because it means not only that absolutely positioned elements saves us from most of the pain when it comes to reflows, but also that we can sometimes avoid reflows entirely, and only be bothered by it's baby brother, the repaint.

    One prime example for this would be when one has an animation. I'm not quite sure what makes a repaint and what makes a reflow, but, one could think that changing the src of an image tag rapidly to animate it produces a reflow, due to images having diffrent sizes. Perhaps browsers are smart enough to avoid reflowing an image that has a set size, but I doubt it. What, however, we can do, is use the overflow animation trick, which I will not go into detail with here about. I highly suspect that this does not produce a reflow, but a repaint, but I can't be sure (and it's damn hard to test -_-)

    The site explains that Opera doesn't do reflows for every DOM change. It tries to wait until the script thread is over (if you don't know what this means, just know that lots of DOM changes after each other will be treated with one single reflow). The exception to this is if the thread takes too long to execute, pushing out the reflow too much. Opera will in this case do the reflow forcefully.

    I currently have no information about other browsers than Opera. My gut feeling tells me IE doesn't do this very nicely, but I'm not sure about Firefox. Could go both ways.

    I'm working on my testing example, to see if doing these things will help the strain on the computer any (trying to bunch up the DOM manipulations to happen all at once). I shall post here with my results.
    Last edited by Drakim; 11-05-2008 at 07:44 AM.

  5. #5
    Join Date
    Apr 2006
    IE 5 had problems with flickering during screen renders, but I have not seen the issue in IE7 (can't remember if it was fixed in IE6 or not)


    This also has something to do with the processing power in your PC. More modern machines can render quickly while older ones can drag and make the flickering more obvious.

  6. #6
    Join Date
    Sep 2006
    Thus far, no real progress.

    I've made a bunch of tests, which you can see here.

    The "Display Enchantment" works by setting the css style display to "none" right before all objects are updated, and then back to "block" when all objects are finished updating for that loop. This was recommended on the Opera website, but I see no improvement at all.

    It could be that the display enchantment only helps when you aren't absolutely positioning elements. But I'm too lazy to check that, because, it's pretty rare that you have moving objects that are not absolutely positioned or fixed. Almost impossible, I'd say.

    The "Swap Enchantment" works by removing the world object (the big green block) right before all objects are updated, and then adding it back to the body after all objects are finished updating for that loop. I was expecting flicker from this method, but there was none (but flicker may result in other browsers than firefox). This method actually made things worse. At 800 objects flying around, I noticed a slight slowdown when using this method.

    I've yet to come up with any more ideas for how to avoid unnecessary reflows and repaints, but I'll add any suggestions you guys might have.

    Oh, and please do my test in your browsers, and report back if there is anything interesting happening.

  7. #7
    Join Date
    Sep 2006
    Okay, I found something interesting.


    Soon, Firefox will allow you to detect a repaint as an event. Although this isn't very usable for web development in general (unless you use it to make Firefox specific enchantments), but, I think it would be awesome to test how repaint behavior in general work (or at least for Firefox)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14313 seconds
  • Memory Usage 2,922KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates