Game runs very slowly - optimization I've made my first simple game in HTML5. Unfortunately, it runs so slowly. But there are only few rectangles! On my PC, frame rate is between 20 and 25. I expected something like 60-70 but no 20! You can see this game here: http://mygames.jcom.pl/ (and you can see code by clicking Ctrl+Shift+C in Chrome). What can I do to speed up my game? Why my (so simple) game doesn't run as smoothly as another (bigger and prettier) games in HTML5?

var gameCanvas = document.getElementById('gameCanvas');
var ctx = gameCanvas.getContext("2d");

var mapa = loadFromFile("level1");
var gracz = new Obiekt("square", 40, 40);
var blokczarny = new Obiekt("square", 80, 80);

var FPS = 0;

var gameWidth = gameCanvas.width;
var gameHeight = gameCanvas.height;
var gameTranslationX = 0;
var gameTranslationY = 0;

// Handle keyboard controls
var keysDown = {};

var graczData = {
    posOnScreenX: gameWidth / 2,
    posOnScreenY: gameHeight / 2,
    fallSpd: 300,
    runSpd: 300,
    jumpSpd: 600,
    fell: true,
    resetjumpSpd: 600,
    isJumping: false,
    yWhenStartedJump: 0,
    kierunek: 1
};



addEventListener("keydown", function(e) {
    keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function(e) {
    delete keysDown[e.keyCode];
}, false);

var reset = function() {
    gracz.y = 0;
    gracz.x = 0;
    ctx.setTransform(1, 0, 0, 1, graczData.posOnScreenX, graczData.posOnScreenY);
};

// Update game objects
var update = function(modifier) {
    this.zmianaX = 0;
    this.zmianaY = 0;
    if (38 in keysDown &amp;&amp; !graczData.isJumping &amp;&amp; graczData.fell) {// Player holding up
        graczData.isJumping = true;
    }
    if (40 in keysDown) {// Player holding down

    }
    if (37 in keysDown) {// Player holding left

    }
    if (39 in keysDown) {// Player holding right

    }
    //ochrona przed "wejsciem w sciane" X
    var newMapPosX = getPositionOnMap(gracz.x + this.zmianaX, gracz.y, blokczarny);
    if (mapa[newMapPosX[1]][newMapPosX[0]] == 0) {
        gracz.x += this.zmianaX;
    }
    //spadanie
    if (getTypeOfTile(gracz.x, gracz.y + (graczData.fallSpd * modifier), blokczarny, mapa) == 0) {
        gracz.y += graczData.fallSpd * modifier;
    } else {
        graczData.fell = true;
    }
    //skakanie
    if (getTypeOfTile(gracz.x, gracz.y - (graczData.fallSpd * modifier), blokczarny, mapa) == 0 &amp;&amp; graczData.isJumping) {
        gracz.y -= graczData.jumpSpd * modifier;
        graczData.jumpSpd -= 500 * modifier;
        graczData.fell = false;
        if (graczData.jumpSpd &lt; 0) {
            graczData.jumpSpd = graczData.resetjumpSpd;
            graczData.isJumping = false;
        }
    }
    //bieganie
    if (getTypeOfTile(gracz.x + (graczData.runSpd * modifier * graczData.kierunek), gracz.y, blokczarny, mapa) == 0) {
        gracz.x += graczData.runSpd * modifier * graczData.kierunek;
    } else {
        graczData.kierunek *= -1;
    }
    //document.write("GraczY: " + gracz.y + "GraczX: " +gracz.x);
};

var clear = function() {
    ctx.fillStyle = "rgb(255, 255, 255)";
    //ctx.beginPath();
    ctx.rect(0, 0, gameCanvas.width, gameCanvas.height);
    //ctx.closePath();
    ctx.fill();
}

var oldgracz = {
    x: 0,
    y: 0
}
var render = function() {

    ctx.translate(-(gracz.x - oldgracz.x), -(gracz.y - oldgracz.y));
    gameTranslationX = gracz.x - graczData.posOnScreenX;
    gameTranslationY = gracz.y - graczData.posOnScreenY;
    clear();

    console.log("FPS" + FPS);

    ctx.fillStyle = "rgb(255, 0, 0)";
    gracz.draw(ctx);
    ctx.fillStyle = "rgb(0, 0, 0)";
    drawMap(mapa, blokczarny, ctx);
    oldgracz.x = gracz.x;
    oldgracz.y = gracz.y;
}

// The main game loop
var main = function() {
    var now = Date.now();
    var delta = now - then;

    FPS = 1000 / delta;

    update(delta / 1000);
    render();

    then = now;
};

// Let's play this game!
reset();
var then = Date.now();
setInterval(main, 100);
I believe your issue is related to the setInterval, you should at least try not to use it; instead use requestAnimationFrame.

for example here is a link:
http://paulirish.com/2011/requestanimationframe-for-smart-animating/

GL!