-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |