Reputation: 1533
I was wondering if we could get the value that a console.time()
prints and store it in a variable or write to a log file or something. Basically, can we have the execution time for purposes other than printing on the console?
Upvotes: 3
Views: 3056
Reputation: 350202
For measuring execution times, there is the performance
object. In its most simple use you would do start = performance.now()
.
But there are more features for measuring intervals, like performance.mark(name)
which creates a timestamp in the browser's performance entry buffer with the given name, and then measure(name, fromname, toname)
which measures the delay between two marks and stores the result with a new name, ...etc.
In nodejs there is process.hrtime.bigint()
, which returns a bigint representing nanoseconds. For older nodejs versions you can use process.hrtime([time])
which returns two integers in an array: [seconds, nanoseconds].
The performance-now
npm module is based on the latter and mimics the performance.now()
behaviour.
Upvotes: 4
Reputation: 1337
You can use Date.now()
in order to retrieve the current time.
> Date.now()
>> 1552371509583
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
Upvotes: 1