Snowman
Snowman

Reputation: 32061

How to time a function in 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?

Upvotes: 28

Views: 18869

Answers (3)

Tur1ng
Tur1ng

Reputation: 834

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 original post.

var startTime = performance.now()
    
doSomething()   // <---- measured code goes between startTime and endTime
    
var endTime = performance.now()

console.log(`Call to doSomething took ${endTime - startTime} milliseconds.`)

Upvotes: 27

cherucole
cherucole

Reputation: 678

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

Upvotes: 9

Mohamed Khalil
Mohamed Khalil

Reputation: 3126

you can use console.time but throw third party packages react-native-console-time-polyfill
otherwise with performance monitor from developer menu Show Perf Monitor

Upvotes: 4

Related Questions