Date.now() resolution in Bangle.js 2

Posted on
  • What is the resolution of Date.now() in Bangle.js 2, i.e. the underlying hardware resolution of datetime.

  • I think I figured this out. If I execute Date.now() - Date.now() in Web IDE console the result is either -0.21362304687 or -0.18310546875.

    Difference between those is 0.03051757812 i.e. 0.00003051757812 ms and
    1/0.00003051757812 = 32768.00002684...

    So resolution seems to be 1/32768 seconds.

  • I am hijack the topick. Currently to get a current time I am using
    var D;
    D = new Date() ;
    I suspect it reallocates the var's memory each time for the new object and can cause memory fragmentation.
    Is there a more efficient way to get current time into same variable instance?

  • So resolution seems to be 1/32768 seconds.

    Yes, that's right! Good detective work :)

    Is there a more efficient way to get current time into same variable instance?

    It's really not that bad... I think allocating Date uses 3 variables, and because of the way Espruino works that's pretty quick and not likely to cause any fragmentation.

    You can do D.setTime(Date.now()) but I think it's of such a marginal improvement it's not worth it.

  • That is great! Thanks Gordon.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Date.now() resolution in Bangle.js 2

Posted by Avatar for malaire @malaire

Actions