View Full Version : Resolved Numbers (Shot of my browser)
keyboard
04-02-2012, 08:15 AM
http://img201.imageshack.us/img201/3268/numw.jpg
<script type="text/javascript">
for(var i=0; i<10000000; i++) {
var randomNumber=Math.floor(Math.random()*100)
document.write(randomNumber + " ");
}
</script>
djr33
04-02-2012, 08:29 PM
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?)
keyboard
04-02-2012, 08:43 PM
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.
... I'm not actually sure how many of the values it printed.
what, you didn't COUNT THEM !?!?!
keyboard
04-03-2012, 12:54 AM
Ummmmm no should I have?
EDIT -
That time it managed to print out 307494 before I had to recover the webpage
Happy Traq? :D
keyboard
04-03-2012, 01:28 AM
Okay -
<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.
djr33
04-03-2012, 01:43 AM
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.
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:
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:
while(1){
document.write(1);
}
djr33
04-03-2012, 02:07 AM
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.
keyboard
04-03-2012, 02:33 AM
Thanks guys :D
mburt
04-03-2012, 10:24 PM
There is a way to do this, but it won't be as fast.
<script type="text/javascript">
var count = function() {
if (typeof i == "undefined") i = 0;
if (i < 1000000) {
i++;
document.getElementById("whatever").innerHTML += i;
setTimeout(function() { count(i); }, 0);
}
}
window.onload = function() {
var div = document.createElement("div");
div.id = "whatever";
document.body.appendChild(div);
count();
}
</script>
I didn't really test it, but I'm assuming that it works.
EDIT: I did test it, it works.
keyboard
04-03-2012, 10:27 PM
But that would just print out one number wouldn't it? I'm trying to print out 100000000 numbers not the number 100000000.
mburt
04-03-2012, 10:31 PM
No check it again, I edited it slightly, it prints out however many numbers you want. It's slower, but it won't crash your browser.
mburt
04-03-2012, 10:40 PM
And I actually figured out a way to make this even faster:
<script type="text/javascript">
var count = function() {
if (typeof i == "undefined") i = 0;
var interval = 100, max = 100;
if (i < max) {
for (var a = i; a < i+interval; a++) {
document.getElementById("whatever").innerHTML += a;
}
i = i+interval;
setTimeout(function() { count(i); }, interval);
}
}
window.onload = function() {
var div = document.createElement("div");
div.id = "whatever";
document.body.appendChild(div);
count();
}
</script>
keyboard, you can mess with those two variables to figure out where the maximum performance lies.
keyboard
04-04-2012, 12:57 AM
Michael, you complete legend :)
Powered by vBulletin® Version 4.2.2 Copyright © 2023 vBulletin Solutions, Inc. All rights reserved.