Code Monkey home page Code Monkey logo

approximate-now's Introduction

approximate-now

Travis build status Coveralls NPM version Canonical Code Style Twitter Follow

Approximate (fast) current UNIX time.

Motivation

Sometimes you don't care about the exact time the event took place, but rather need to know approximately when. If your use case is performance sensitive, then Date.now() might not be the best option because it has a measurable impact to the performance. approximate-now provides an approximate (to the error of 50ms) time without a performance penalty of Date.now().

Usage

import {
  approximateTime,
} from 'approximate-now';

// `approximateTime.now` value is updated by reference every 50ms.
console.log(approximateTime.now);

Sequence guarantee

It is guaranteed that two consequent attempts to retrieve time within the same 50ms time window will have unique values, i.e.

Assuming that the current time is 0000000000000, then:

approximateTime.now; // 0000000000000
approximateTime.now; // 0000000000001
approximateTime.now; // 0000000000002

However, if approximateTime.now is accessed more than 50 times within the same 50ms time window, then the top-most value within that interval is returned repeatedly.

approximateTime.now; // 0000000000000
approximateTime.now; // 0000000000001
approximateTime.now; // 0000000000002

// approximateTime.now x 50
approximateTime.now; // 0000000000050
approximateTime.now; // 0000000000050
approximateTime.now; // 0000000000050

benchmark

Date.now()          x 19,900,411 ops/sec ±0.59% (93 runs sampled)
approximateTime.now x 82,420,291 ops/sec ±0.83% (92 runs sampled)

Use cases

Any sort of time-sensitive operations when it is sufficient to retrieve an approximate timestamp, e.g. roarr logger uses approximate-now to record the time when a log message was produced.

approximate-now's People

Contributors

0xflotus avatar gajus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

0xflotus ra80533

approximate-now's Issues

Optimization

There's an opportunity to optimize a majority of the calculations in the library. Namely, the assignments to maxNow:

let maxNow = 50 * Math.ceil((now + 50) / 50);

let now = 50 * Math.ceil((now + 50) / 50);
let maxNow = now + 50;

  1. t0 := scale × ceil( x0scale )
    where x0 is t

  2. t1 := scale × ceil( x1scale )
    where x1 is t0 + scale

Using the definition of t0:

t1 = scale × ceil( x0scale ) + scale
t1 = t0 + scale

Regarding lock files

There isn't a lock file in this repository. It appears that you don't typically use them in any of your projects. Is there a particular reason for that? I didn't want to commit a lock file without knowing first.

API access differs from that of Date

Is it intentional for the package's API access to differ from that of Date? I initially expected the package to work as a drop-in replacement of Date.now() by exporting a function by the name of now() rather than an object with a property by the name of now.

Is this something you'd allow a PR to change? I have a branch with performance increases adding up to a little over 100%. I'd like to create a PR for it but it's based on an API refactor that would close this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.