Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to time a function in React Native?

Tags:

react-native

This question suggests using console.time, but that isn't available in React Native. Is there a built in way to measure how long a function call takes, without using any third-party packages?

like image 825
Snowman Avatar asked Sep 16 '17 14:09

Snowman


People also ask

How do you delay a function in react native?

Define a function that takes the number of milliseconds as parameter. Use the setTimeout method to resolve a Promise after the provided number of milliseconds.

How do I make time in React native?

To get the current datetime in react native has the Date() object it will return every time the current date and time when we use it. we have to just write new Date() and it returns the date object where we get a year, month, date, hours, minutes, and seconds.

How do I use console time in React native?

Starts a timer you can use to track how long an operation takes. When you call console. timeEnd()/console. timeLog() with the same name, the react-native will output the time, in milliseconds, that elapsed since the timer was started.


2 Answers

I ran into the same error but managed to fix it easily, just enable debugging on your app to use chrome or react native debugger. From the console of these debuggers, the console.time() and console.timeEnd() is supported and works perfectly

like image 80
cherucole Avatar answered Nov 10 '22 02:11

cherucole


Using react-native v0.63 (not sure about lower versions), you can use the performance api, which is described in the question you linked in the OP.

var t0 = performance.now()
    
doSomething()   // <---- measured code goes between t0 and t1
    
var t1 = performance.now()
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
like image 38
Tur1ng Avatar answered Nov 10 '22 01:11

Tur1ng