2

I observed number of instruction counts executed on cpu while running javascript tests on Android browser.
The test js codes are simple in html. The files are in android local directory, not in webserver.

In html:

<html> 
    <head>
       <script type='text/javascript' src='test.js'>
    </head> 
    <body> 
       <div id='content'> ... </div>
       <span> .. </span>
       <div id='logo'> ... </div>
       ...
    </body> 
</html>

In test.js:

   for (i = 0; i < 1000 ; i++) {
        ... 
           $().append("<div id='content2'> ... </div> 
           var temp1 = $(span#content2)
           var temp2 = $(#logo) 
           var temp3 = $(h3.id)
        ...
    }

In a nutshell, in test.js, simple methods such as .append() using jQuery are used. When I run this simple test code on Android browser, I think I should have consistent number of the instructions, but, actually, I have various instruction counts in each run. It may have some pattern in the variation, but, not seem clear.

My initial guess was an "asynchronous" thing in DOM in html with javascript. So, I changed the html like this:

<html> 
    <head>

    </head> 
    <body> 
       <div id='content'> ... </div>
       <span> .. </span>
       <div id=''> ... </div?
       ...
    </body> 
     <script type='text/javascript' src='test.js'>
</html>

Also, I tried this

$(document).ready(function() { 
   ... 
       $().append("<div id='content2'> ... </div> 
       var temp1 = $(span#content2)
       var temp2 = $(#logo) 
    ...
});

But, I didn't get consistent number of instructions yet. This is caused by asynchronous problem? Or jQuery methods in terms of DOM have kinda indeterministic behavior in run time? Or, JS JIT compiler has very different behavior in each run? Can anyone give me some clue?

Thanks!

1 Answer 1

1

It's not just that JavaScript does not run synchronously with DOM rendering, it's that HTML parsing itself is inherently loose, and was never designed to be deterministic. "Good enough" is what got JavaScript and HTML to where every device and its brother can use them, but the price paid for that is some level of unpredictability.

In general, don't use HTML parsing if you don't have to, especially if you want consistent performance. The DOM has a perfectly serviceable createElement method that you can send to jQuery's append method, which will likely get you more consistent results. (And it's not like insertBefore is all that hard a method to use. If you're taking the time to measure CPU cycles, you're doing a level of work significantly greater than jQuery's target model.)

(And I'd be very surprised if a JIT compiler didn't give you different results over a large enough test run. Unless you manage to have exactly the same device each time, I'd expect some variation based on the available input variables it would have to use to judge how much compilation to do, such as memory allotment and CPU load.)

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, I'm interested in CPU workload and performance.If HTML parser is loose and non-deterministic, as you mentioned, I'm highly likely to have larger variance in libdvm.so which is dalvik library, (supposing html parser is implemented with Java) rather than libwebcore.so. Let me get more data with experiment and look into profile in detail, then I will post it. By the way, I'm not much familiar with html parsing. So, could you explain why it is inherently loose and unpredictable? Thanks, again!
html parsing is loose because of tag soup. when constructing a DOM document in memory, the parser has to allow for any random tag to be the next bytes read. I'm not sure I'd go so far as to call it "unpredictable", but it is definitely non-deterministic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.