Skip to content

Commit

Permalink
Update timestamp.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 23, 2017
1 parent ad687ca commit f58801b
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions docs/date/timestamp.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
# Creating timestamp

You have two ways of creating a timestamp:
We have different approach for measure time, depending on the environment where we are running the code.

- `Date.now()`
- `new Date().getTime()`
## Using `Date`

The universal way.

Althought `Date.now()` and `new Date()` have the same behavior, `Date.now()` is faster because you are not allocating an object and then calling the method of the object.
Using `Date` we can perform two actions for the same purpose:

- `Date.now()`
- `new Date().getTime()`

!> In the browser side, consider using [performance.now()](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now), which has better precision than `Date.now()` and always increases at a constant rate, independent of the system clock.
Althought both have the same output, but `Date.now()` is faster because you are not allocating an object and then calling the method of the object.

This is specially remarkable when you make successive calls (for example, when you append timestamps in logs).

Another thing to consider is the fact that, when you create a `new Date()` you are linking a specific point in time with the object. Therefore, successive calls to `.getTime()` will have the same output.
Another thing to consider is the fact that, when you create a new Date() you are linking a specific point in time with the object. Therefore, successive calls to .getTime() will have the same output.

```js
var time = new Date()
time.getTime() // => 1472153262516
time.getTime() // => 1472153262516
```

The limitation of use `Date` is that the mimimum quantity of time is limited to one-millisecond resolution, while using browser/server API's methods for high resolution timming you can get resolution in order of nanoseconds.

## Using `perfomance.now`

The [performance.now()](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) perform a high resolution time accurate to five thousandths of a millisecond (5 microseconds).

## Using `process.hrtime`

THe [process.hrtime](https://nodejs.org/api/process.html#process_process_hrtime_time) returns high-resolution real time in *[seconds, nanoseconds]* `Array`.

The time `Array` is relative to an arbitrary time in the past (not related to the time of day) and therefore not subject to [clock drifts](https://en.wikipedia.org/wiki/Clock_drift).

0 comments on commit f58801b

Please sign in to comment.