Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Numbers (Shot of my browser)

  1. #1
    Join Date
    Mar 2011
    Posts
    2,171
    Thanks
    61
    Thanked 121 Times in 117 Posts
    Blog Entries
    4

    Default Numbers (Shot of my browser)



    Code:
    <script type="text/javascript">
    for(var i=0; i<10000000; i++) {
    var randomNumber=Math.floor(Math.random()*100)
    document.write(randomNumber + " ");
    }
    </script>
    Last edited by keyboard; 04-04-2012 at 08:45 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This is what recursive functions are useful for. On the other hand, that would be a great way to guarantee crashing your browser. (Did 10000000 not crash it?)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Mar 2011
    Posts
    2,171
    Thanks
    61
    Thanked 121 Times in 117 Posts
    Blog Entries
    4

    Default

    Yep, sometimes it crashed and sometimes IE stopped it because the script took to long. I'm not actually sure how many of the values it printed.

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    ... I'm not actually sure how many of the values it printed.
    what, you didn't COUNT THEM !?!?!

  6. #6
    Join Date
    Mar 2011
    Posts
    2,171
    Thanks
    61
    Thanked 121 Times in 117 Posts
    Blog Entries
    4

    Default

    Ummmmm no should I have?

    EDIT -

    That time it managed to print out 307494 before I had to recover the webpage

    Happy Traq?
    Last edited by keyboard; 04-03-2012 at 01:03 AM.

  7. #7
    Join Date
    Mar 2011
    Posts
    2,171
    Thanks
    61
    Thanked 121 Times in 117 Posts
    Blog Entries
    4

    Default

    Okay -

    Code:
    <html>
    <head>
    </head>
    <body>
    <div id="display"></div>
    <input type="button" onclick="count()" />
    <script type="text/javascript">
    for(var i=0; i<100000000; i++) {
    var randomNumber=Math.floor(Math.random()*100)
    var getID = document.getElementById('display');
    getID.innerHTML = getID.innerHTML + randomNumber + " ";
    }
    function count() {
    var getID = document.getElementById('display');
    var numNum = getID.innerHTML.split(' ').length;
    alert(numNum - 1);
    }
    </script>
    </body>
    </html>
    but know it'll only print around 25000 before IE9 says it's running for to long and stops it.

    Anyone got a suggestion to make it run faster or print more values?

    I'm only fiddling with this for fun so mods, please don't move this thread to a different forum.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Nope. That's the memory limit for your browser. You can close all other applications, buy more RAM, and potentially switch to a more efficient browser (not sure which is best), but in the end it'll still have a maximum amount.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Though, Daniel, correct me if I'm wrong, I think you can reduce the amount of memory used by opening up your variables. For example, something like the following:
    Code:
    for(var i = 0, getID = document.getElementById('display'); i < 100000000; i++){
    	getID.appendChild(document.createTextNode(Math.floor(Math.random()*100) + " "));
    }​
    (only defining getID once, never defining randomNumber, using appendChild and createTextNode because that way the browser doesn't need to render anything, therefore faster).

    @keyboard
    I'm just as scared to run this as I am:
    Code:
    while(1){
    document.write(1);
    }

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can optimize it, but it's still going to max out. I also expect that it will get you smaller and smaller improvements because holding all of that in memory (just the text of the page even) will eventually be too much for the computer and compound on itself.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •