Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 5 of 5

This is a discussion on inconsistent function result within the Bugs and Vulnerabilities section, part of the Google Chrome category: I have a javascript function that gives very different results in Chrome than it does in Safari or Firefox (it ...


  1. #1
    jaschulz is offline Junior Member
    Join Date
    Jan 2010
    Posts
    12

    Default inconsistent function result

    I have a javascript function that gives very different results in Chrome than it does in Safari or Firefox (it doesn't work at all in IE at the moment, but I am not worried about that).

    The purpose of the function is to scan a (fairly long) text file and identify and list the 64 most frequent character pairs in that text. This involves both Ajax and regex calls.

    I am using this function to scan a list of 25 text which were previously scanned with another function. The point of this exercise is to see if the two functions product identical results.

    So, I use the new function to scan each text and then compare that result to a "library" of results from the older function. Safari and Firefox give identical results, but, in Chrome, the result is different.

    I suspect the problem has something to do the the asynchronous character of javascript functions (and the fact that both loading and scanning the different texts require more or less time depending on the length of the text in question). I tried to cover that possibility by implementing a timing system that would make sure that the browser had time enough to finish both the Ajax call and the regex scan for each text before the next text was scanned.

    It works like this:

    Code:
    var index = 0;
    var round = 1;
    var limit = 4;
    var interval = 1000;
    var tryDoingIt = null;
    var doingIt = false;
    var http_request = false;

    function doIt() {
    tryDoingIt = setInterval('tryOne()', interval*round);
    }

    function tryOne() {
    //doingIt is set to true in function doOne()
    //and then set to false in function showAjaxText;
    //the idea being to allow the current text to be loaded
    //and scanned before the next one is called
    if (!doingIt) {
    if (index < titlesArray.length) {
    doOne();
    } else {
    clearInterval(tryDoingIt);
    tryDoingIt = null;
    index = 0;
    if (++round > limit) {
    alert('Done!');
    } else {
    doIt();
    }
    }
    }
    }

    function doOne() {
    doingIt = true;
    getAjaxText(titlesArray[index],'contentDiv','./texts/','ISO-8859-1');
    }

    function getAjaxText(content, target, textPath, characters) {
    http_request = false;
    // running locally on IE5.5, IE6, IE7
    if ((location.protocol=="file:")&&(window.ActiveXObject)) {
    if (!http_request) try {
    http_request=new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
    http_request=false;
    }
    if (!http_request) try {
    http_request=new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
    http_request=false;
    }
    }
    // IE7, Firefox, Safari, Opera...
    if (!http_request) try {
    http_request=new XMLHttpRequest();
    } catch(e) {
    http_request=false;
    }
    // IE6
    if (typeof ActiveXObject != "undefined") {
    if (!http_request) try {
    http_request=new ActiveXObject("MSXML2.http_request");
    } catch(e) {
    http_request=false;
    }
    if (!http_request) try {
    http_request=new ActiveXObject("Microsoft.http_request");
    } catch(e) {
    http_request=false;
    }
    }
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    if (http_request.overrideMimeType) { http_request.overrideMimeType('text/plain; charset=' + characters); }
    }
    if (!http_request) {
    alert('ERROR: Cannot create an XMLHTTP instance');
    return false;
    }
    http_request.onreadystatechange = function() { showAjaxText(content, target); }
    http_request.open('GET', textPath + content, true);
    http_request.send(null);
    }

    function showAjaxText(content, target) {
    if (http_request.readyState == 4) {
    if (http_request.status == 200 || http_request.status == 0) {
    var rDiv = document.getElementById('resultDiv'+parseInt(round));
    var resultStr = http_request.responseText;
    var pairsStr = countAllPairs(resultStr);
    pairsStr = pairsStr.substr(0,pairsStr.length - 1);
    var oldPairsStr = oldPairsArray[index];
    if (pairsStr === oldPairsStr) {
    showMatch();
    } else {
    showMismatch();
    }
    index++;
    doingIt = false;
    } else {
    //alert('Error: Code(' + httpReq.status + ')');
    }
    }
    }
    The idea (in the above) is that the demo (which you can see at: www.wiseware.com/test) goes through the list of 25 titles four times, each time increasing the length argument of setInterval. Safari and Firefox are indifferent to this timing business and always produce the same result, but Chrome not only produces a different result than the other two, it also produces different results in each of the four "timing rounds".

    Can anybody help me figure out what is going on here?

    Thanks,

    JAS

    P.S. One of the texts ("The_Good_Soldier") was deliberately altered so as to guarantee at least one "mismatch" result in this trial.

  2. #2
    jaschulz is offline Junior Member
    Join Date
    Jan 2010
    Posts
    12

    Default unstable sort()

    I have found the root of at least part of my problem: the javascript sort() function is not "stable" in Chrome.

    My app (described in my original post) scans a text and computes the frequencies of all possible character pairs.

    The initial order of the resulting array is alphabetical:

    Code:
            for (var i = 0; i < 26; ++i) {
              for (var ii = 0; ii < 26; ++ii) {
                countPairs(alphabetStr[i], alphabetStr[ii]);
              }
            }
    The array is then sorted by the frequency of the pairs. This is where the problem occurs. In Chrome, pairs with identical frequency appear in the sorted array in reverse alphabetical order! Only Chrome does this; see the attached pdf. (The critical cases are marked with asterisks.)

    I could fix this problem if I could sort first by the frequency key and then by the alphabetical key, but I don't understand how to do that sort of thing with the javascript sort() function. Can anyone help me here?

    Thanks,

    JAS
    Attached Files Attached Files

  3. #3
    Luc VN's Avatar
    Luc VN is offline Senior Member
    Join Date
    Sep 2008
    Location
    Belgium - Europe
    Posts
    758

    Default

    Thanks for coming back with your findings Jas

  4. #4
    jaschulz is offline Junior Member
    Join Date
    Jan 2010
    Posts
    12

    Default

    Well, I can write a sort function that solves my problem, but what I don't understand is how the sort() in Chrome can be unstable when the sort() in Safari is stable and both are based on web-kit?

    JAS

  5. #5
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    Javascript isn't handled by WebKit in Chrome, Chrome uses V8.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

Similar Threads

  1. Does chrome's download function is multithreading?
    By RedNada in forum Chrome Talk
    Replies: 0
    Last Post: 04-28-2010, 02:15 PM
  2. Chrome Extension function
    By Bhav in forum Plugins Development
    Replies: 1
    Last Post: 02-10-2010, 08:16 PM
  3. Could Invoke function of NPAPI return multiple outputs?
    By wadewu in forum Plugins Development
    Replies: 0
    Last Post: 12-29-2009, 06:41 AM
  4. How to invoke function in chrome extensions
    By rohit in forum Plugins Development
    Replies: 7
    Last Post: 12-11-2009, 06:33 AM

Posting Permissions

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