Is context.clearRect() necessary? My application is a bit slow. I believe this is because .clearRect is one of the more intensive events in my application so far. However I also draw an image on top of the screen (as background) at the top of each loop, like this:

function redraw(){
  ctx.save();
  ctx.clearRect(0,0,canvas.clientWidth,canvas.clientHeight);
  ctx.drawImage(background,0,0,canvas.clientWidth,canvas.clientHeight);

  var i=0,l=objects.length;
  while(i&lt;l){
    objects[i].redraw();
    i++
  }
  ctx.restore();
}


I'd like to improve the speed of this code. Would removing the .clearRect call be a good way to optimize performance? Or will this clog up memory?
The way it's phrased, this isn't really a Code Review question. Still, I'll try to treat it as one.

But first, to answer your question: Yes, I would imagine you could just remove the call to clearRect as long as the background image you're drawing is entirely opaque.

(incidentally, you should just be able to try it out and see quicker than asking question on a Stack Exchange site: Just comment out the line and see what happens.)

However, in code review terms, I have to wonder why you're drawing the image on every update. If the background image doesn't constantly change, you could just consider keeping the clearRect call, but setting the background image as a regular CSS background on the canvas element. Then you don't need to worry about drawing the background yourself; it'll just be there, behind the canvas element. 

Also, what's with the while loop? You've just written a complicated equivalent to a for-loop. Given its purpose here, a for loop would by far be the most straightforward thing to use:

for(var i = 0, l = objects.length ; i &lt; l ; i++) {
  objects[i].redraw();
}


While you're at it, please give your code some room to breathe. Apart from indentation, I don't see a single space anywhere in your code. It's all squished together, which makes it unnecessarilyhardtoread.

And you're missing a semi-colon in the while loop. JS will still run your code, but I see no reason to rely on JS's automatic semi-colon insertion. Especially not on the one line that must work to avoid an infinite loop.