I am writing some debug code in a real-time javascript app. In the update loop, I want to:
- get current time in milliseconds
- pare to last frame's time and print out framerate
- set last frame's time to current time from the variable above
All straightforward, except that since it's in such a performance-critical piece of code, I'm trying not to call
var d=new Date();
every frame before I call
thisFrameTime = d.getTime();
Is this possible? Is there something along the lines of:
d.now
which updates the time in the existing date object?
My thinking is that I want to stay away from memory allocation / gc while in debug mode so it impacts framerate less - but maybe that's just not how it's done in javascript? (My background is more C/C++, so maybe this is not the right way of thinking for JS?)
I have searched Google and Stack Overflow, and can't seem to find an answer, which makes me think it's not possible. If that's the case, confirmation would be helpful.
Would love any thoughts - what's the most performant way to get this done?
I am writing some debug code in a real-time javascript app. In the update loop, I want to:
- get current time in milliseconds
- pare to last frame's time and print out framerate
- set last frame's time to current time from the variable above
All straightforward, except that since it's in such a performance-critical piece of code, I'm trying not to call
var d=new Date();
every frame before I call
thisFrameTime = d.getTime();
Is this possible? Is there something along the lines of:
d.now
which updates the time in the existing date object?
My thinking is that I want to stay away from memory allocation / gc while in debug mode so it impacts framerate less - but maybe that's just not how it's done in javascript? (My background is more C/C++, so maybe this is not the right way of thinking for JS?)
I have searched Google and Stack Overflow, and can't seem to find an answer, which makes me think it's not possible. If that's the case, confirmation would be helpful.
Would love any thoughts - what's the most performant way to get this done?
Share Improve this question edited Feb 14, 2013 at 13:40 J. Steen 15.6k15 gold badges58 silver badges64 bronze badges asked Dec 2, 2011 at 19:54 GameMakerGameMaker 951 silver badge7 bronze badges2 Answers
Reset to default 4There is a Date.now() function.
var time = Date.now()
The problem is that it is a part of EcmaScript 5 so older browser (IE 6-8) don't support it. As written on the MDM (link above) you can solve this issue by including this into your code:
if (!Date.now) {
Date.now = function() {
return +(new Date);
};
}
You don't control gc in a browser, it runs when it runs. Creating Date objects each time you need the current time is probably the best way to do this and should be trivial unless you're holding references to the objects (which will prevent them from collecting).
However, you should probably use AOP-style profiling code instead of baking "debug code" into your source. I'm not even sure what that is, but it sounds like something you should never do.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627008a4636869.html
评论列表(0条)