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?
Asked
Active
Viewed 1.3k times
25

Snowman
- 31,411
- 46
- 180
- 303
-
What version of RN are you using? Because I always use `console.time` and never faced any error whatsoever – Ray Sep 16 '17 at 15:43
3 Answers
21
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.`)

Tur1ng
- 745
- 8
- 22
-
-
1@loekTheDreamer https://developer.mozilla.org/en-US/docs/Web/API/Performance/now – Tur1ng Jun 30 '22 at 04:33
-
it is still available in 2022, but for some reason using typescript it doesn't recognize it as being available. – ICW Aug 16 '22 at 02:22
-
for eslint warning "'performance' is not defined" you need to add `performance: true` into `globals` object in eslint configuration file (for example, .eslintrc.js) – Vasyl Nahuliak May 12 '23 at 07:52
10
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

cherucole
- 560
- 7
- 15
4
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

Mohamed Khalil
- 3,036
- 1
- 20
- 26