views:

47

answers:

1

hi guys , i was optimizing my html5 game engine for performance issues and i want to know how much time a render process needs.So i got a bunch of render functions.Each of them render seperated parts of the game.. such as blocks , players etc.

function gameRender() {
    var d1 = new Date();
    var firstTime = d1.getTime();

    // render stuff

    var second = d1.getTime();
    console.log("Renders took " + (second-firstTime));
}
+4  A: 

Hmm I got the problem: after a little search on Google , I realized that I must use a second date object for the second variable, so here is a fixed version:

function gameRender() {
    var d1 = new Date();
    var firstTime = d1.getTime();

    // render stuff

    var d2 = new Date();
    var second = d2.getTime();
    console.log("Renders took " + (second-firstTime));
}
Eren Yagdiran
You can also use "new Date().getTime()" if you're not using the Date objects for anything else.
Turnor
@Turnor: You do not need the `getTime` either. When doing an operation (like addition or subtraction) on those objects, the `valueOf` method will be called. The `valueOf` method for `Date` objects returns the same value as `getTime`.
CD Sanchez