Hrtime in ionic

The touchstart and touchend events in Ionic currently return a timeStamp which is not, but (seeing the value and its sub-ms resolution) it is I’d think based on process.hrtime() or similar (equivalent to in the browser).
My question is: how can I simply call this exact same timestamp (without the events) so that I would have a base comparison to the event timestamps?

I’d use it e.g. like this:

start = now(); // this "now()" is what I'm looking for
// things happen
// (touchstart)="touchstart($event);" is triggered
touchstart(event) {
        time_elapsed = event.timeStamp - start;

By the way I tried and I can actually call the process variable, but it has no hrtime property.

What r u trying to achieve?

Debounce input from user?

no. it’s a reaction-time task and I’d need to store the “time_elapsed” variable from the example (more precisely: the time between the appearance of one element and the touchstart event)

That is indeed a different case then I had in mind

For measuring time I read there are multiple ways

But this u may alreasy know

What I would try is to measure the difference between event-time and my own time and use this calibration to calculate the actual response time in the reaction game

Not sure if this helps

Alternative to using the event data is to handle the reaction time through the (click) event handler itself



Thanks for the tips, but yeah, this I knew. But cannot be simply called from Ioinic (as far as I know, but tell me if there is a way), and I also tried a related nodejs package ( but had trouble using it with Ionic. Of course in the worst case I can always resort to and just call it in the event handler function as you said, but I’m still looking for a better way (using the timeStamp and hrtime).

But still, what I don’t get is how the touchstart gets its timeStamp value. If it can call it then why can’t I in my code? I even tried to go through the ionic-3 source code but all I see is everywhere. So either it’s outdated or I’m missing something.

don’t see why cannot be called plain vanilla without packages

Check the constructor in home.ts

But that doesn’t answer your valid question on the timeStamp implementation in Ionic. Makes me curious too…

Update: the github repo on ionic is now in Ionic 4 code. But here you see:

export function now(ev: UIEvent) {
  return ev.timeStamp ||;


Called by for instance by (search for touchstart)

Which seems to indicate it either uses the timeStamp generated by the browser (assuming that is the argument passed) or generate one using Date.nowI().

You are totally right, does work for me too, I have no clue what I did wrong previously that it didn’t. And I will simply use that as a baseline since it seems equivalent to touchstart’s (or UIEvent’s) timeStamp:

Thanks very much for the help.

1 Like