Does anyone know how much overhead to expect from the Javascript interpreter?
I have been working on the Game of Life (without React as SASS since I have not yet broken down to try and learn them from other sites). I figure that this way I wwill have a working framework and just adapt it to use React and SASS once I learn them. But that’s not my current issue…
In an effort to get it as fast as possible (higher framerate) I put in some times to see where the bottlenecks might be. This function starts and pauses the game:
function startStop() {
var start = Date.now();
if ($("#btnStartStop").html() === "Start") {
timer = window.setInterval(function(){
getNextGen();
displayBoard();
genNr++;
var end = Date.now();
times.push((end - start));
start = end;
}, 10);
$("#btnStartStop").html("Pause");
} else {
window.clearInterval(timer);
$("#btnStartStop").html("Start");
console.log("Average: " + average(times));
console.log("Std Dev: " + standardDeviation(times));
console.log("Count: " + times.length);
}
}
Originally, I had start being set at the beginning of the timer loop so that I could determine just how long each pass through the loop was taking. I was getting an average of about 60ms with a Std. Deviation of about 8.8ms. That should result in a framerate of about 16 or 17 fps, but I was getting closer to 5.5 fps. That is when I changed the code to the above, to get the actual time between frames. The results are:
Average: 178.4709388971684
Std Dev: 34.98488460723234
Count: 671
which matches the framerate that I was getting. Does javascript really have that much overhead? The actual framerate is about 1/3 what I would be getting based on the time spent in each iteration of the loop? That can’t be right. Before I added the last few items I was getting a framerate of almost 10 fps. With the overhead that something is taking now, I would have to get through my loop in negative time to get 10 fps and I have seen versions made by some of the other campers (using React and SASS) with significantly higher rate than that. Is there something that I can do to speed things up a bit?