SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question javascript & memory

    hi, all.

    we got some scripts that are pretty heavy, and sometimes the thing is thinking so hard that it'll freeze the browser.

    are there any ways, like a library or something to increase memory?

    thanks, any help appreciated.

    T.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe this is a CPU problem rather than memory!

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE 6 is well known to leak pretty hard. It is a bit better in version 7 though. You might wanna check out this article on how to avoid leaks.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi all and thanks for your replies.

    the script is just really slow. not sure if it's memory-related. maybe just too many computations.

    are there any libraries to make javascript execute faster in general that you guys know of?

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it all depends on how your code is written.

    Care to show some of your scripts?

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by Pepejeria View Post
    No, it all depends on how your code is written.

    Care to show some of your scripts?
    hi, thatnks for your reply.
    unfortunately my boss would kill me if he knew I posted code here

    I think the prob is that we got really long loops, but the script show the result on the browser when the whole thing is done, so the whole thing takes forever and the user has to wait with no feedback.

    consider this:

    Code JavaScript:
    for (var i=0; i<999; i++) {
    var myImg = document.createElement('img');
    myImg.src = '/images/img.jpg';
    document.appendChild(myImg);
    }

    i was thinking, would using setTimeout speed up things? the user would see the page populate with images gradually, giving some feedback that the script is 'thinking'.

    something like

    Code JavaScript:
    function process() {
    var myImg = document.createElement('img');
    myImg.src = '/images/img.jpg';
    document.appendChild(myImg);
    } 
     
    setTimeout('process()', 1);
    thanks for the help!!!

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The DOM is usually slow in browsers, especially in IE. Using innerHTML can speed things up. See this testcase to see the difference in speed when using different methods to achieve the same thing.

    It all depends on your code I would say. There are tons of tricks to speed things up. I don't know where to start since I don't know how your code looks like.

    Lets take a common example:
    Code:
    var nodeList = document.getElementsByTagName("div");
    
    for (var i=0; i<nodeList.length; i++) {
        // do something with nodeList[i]
    }
    A faster way to iterate the node list would be the following
    Code:
    var nodeList = document.getElementsByTagName("div");
    
    var node;
    
    for(var i=0; (node = nodeList[i]); i++)
        // do something with node
    }
    But again, it all depends on what you do.


Bookmarks

Posting Permissions

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