Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:19:26+00:00 2026-06-04T14:19:26+00:00

Does JavaScript provide a high resolution timer? I’ve written a few game engines from

  • 0

Does JavaScript provide a high resolution timer?

I’ve written a few game engines from scratch, some in C, some in Java, and some in Flash. I always follow the same basic model when it comes to animations and interactive graphics. Create a basic class/structure with the following design:

void init() { /* Called once, preload essential resources here. */ }
void update(double time) { /* Updates game/animation state using high resolution time. */ }
void render(double time) { /* Updates screen graphics using high resolution time. */ }

void run()
{
    double time;
    init();
    while (!done)
    {
        time = queryTime();
        update(time);
        render(time);
    }
}

Time is so important to smooth animations and game state calculations. In native code Windows, I use QueryPerformanceCounter() and QueryPerformanceFrequency() to perform the role of queryTime() in each game loop and pass the time to update/render. In Java, I use System.nanoTime().

What’s the equivalent in JavaScript? That is, some function like queryTime() which returns a time value with a high degree of accuracy (sub millisecond). From what I’ve heard the best accuracy you can get in JavaScript is about 15 ms … which is horrible for animation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:19:27+00:00Added an answer on June 4, 2026 at 2:19 pm

    Almost all modern browsers provide a high resolution timer. It’s the “High Resolution Time” W3C standard: http://www.w3.org/TR/hr-time/#sec-DOMHighResTimeStamp.

    It allows you to get a sub-millisecond accurate timestamp by calling window.performance.now(). This returns a time stamp in ms, but it is a float so you still get sub-millisecond resolution.

    Very old browsers may implement a “prefixed” version of this standard, e.g. WebKit based browsers used to implement window.performance.webkitNow()

    Here is some code you can use to get the the accurate timestamp when available and fallback to standard precision otherwise:

    if (window.performance.now) {
        console.log("Using high performance timer");
        getTimestamp = function() { return window.performance.now(); };
    } else {
        if (window.performance.webkitNow) {
            console.log("Using webkit high performance timer");
            getTimestamp = function() { return window.performance.webkitNow(); };
        } else {
            console.log("Using low performance timer");
            getTimestamp = function() { return new Date().getTime(); };
        }
    }
    
    getTimestamp();
    

    Note that this getTimestamp() function does not return a value that represents the current date/time. The returned value can only be used to measure time-periods, by subtracting two different timestamps.
    E.g.

    var t1 = getTimestamp();
    //... some other code
    var t2 = getTimestamp();
    console.log("Time delta: " + (t2 - t1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Aside from the language differences Javascript vs. Objective-J what benefits does Cappuccino provide over
I thought Javascript does not have viruses like Java applets do. I turned off
Does the ASP.NET ClientServerManager provide some method or property to return the name of
I'm using a component written in JavaScript. The component is exposing some events. The
Does Javascript or jQuery have sometime like the in statement in Python? a in
What does JavaScript pull out with: screen.width and screen.height on the ipad 3? Is
There is a similar question asked in this post: Does javascript work on mobile
I would like to know how much time (in percent) does JavaScript development takes
Does jquery/javascript have any event model that you can attach to your objects? Basically
Since Javascript does not have any functionality by itself to communicate over raw sockets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.