admin管理员组文章数量:1026249
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.
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.
本文标签: Get current time from existing javascript Date() objectStack Overflow
版权声明:本文标题:Get current time from existing javascript Date() object? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745627006a2159924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论