1 Replies - 660 Views - Last Post: 10 December 2013 - 10:49 PM

#1 TheDianamu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 13

Looking for a way to make this script run faster on the web

Posted 10 December 2013 - 06:59 PM

Hi, I wrote a script a while ago for my game, and now that I have up-sized the map, I need to be able to make the script run faster because drawing 500x500 tiles on the page isn't that speed efficient.

I have been told to replace the for loops with some of type of loop that isn't using the FOR in it, but I got lost in finding a way to do so.

If you wouldn't mind looking of the script below and appointing me in a direction where I can make this script run faster that would be great.

var tiles = [];        
        var draw = 0;
        var w = 0;
        var h = 0;

        $(document).ready(function() {
            $('#tile-selector img').click(function() {
                draw = $(this).attr("tile");
            });
        });
         
        function run() {
            w = $("#w").val();
            h = $("#h").val();
           
            for (var i = 0; i < h; i++) {
                    tiles[i] = [];
            }

            for (var y = 0; y < h; y++) {
                    for (x = 0; x < w; x++) {
                            $("#box").append("<img src='src/" + draw +".gif' x='"+ x +"' y='"+ y +"' height='31' width='31'></img>");              
                            tiles[x][y] = draw;
                    }
                    $("#box").append("<br />");
            }

             $("#box img").click(function() {
                    $(this).attr("src", "src/" + draw +".png");
                    tiles[$(this).attr("x")][$(this).attr("y")] = draw;
             });
        }
        function end() {
            var output = "";
            for (var x = 0; x < w; x++) {
                for (var y = 0; y < h; y++) {
                    if(tiles[x][y] > 0){
                        output += "tiles["+ x +"+"+ y +"*width] =" + tiles[x][y] +";\r\n";
                    }
                }    
            }
            $("#output").val(output);
        }



Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Looking for a way to make this script run faster on the web

#2 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Looking for a way to make this script run faster on the web

Posted 10 December 2013 - 10:49 PM

To check performance of your script,you can use "profiler" in firefox or simple date object
var start = +new Date();  // log start timestamp
function1();
var end =  +new Date();  // log end timestamp
var diff = end - start;


Performance Check
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1