Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Perf work on React-Native?

Currently running Perf on React-Native yields error and the package seems to depend on the DOM. https://facebook.github.io/react/docs/perf.html#using-perf

But still its mentioned in the React-Native docs? https://facebook.github.io/react-native/docs/performance.html#profiling

I have not succeeding in running it at least.

like image 887
ptomasroos Avatar asked Mar 27 '26 20:03

ptomasroos


1 Answers

Yes, I found the similar issue, it seems to me react-addons-perf only work on react, for react native project, you can use RCTRenderingPerf which is a built-in tool in react native lib. My react native version is "react-native": "^0.45.1"

import PerfMonitor from 'react-native/Libraries/Performance/RCTRenderingPerf';

Start measurements

PerfMonitor.toggle();
PerfMonitor.start();

Stop measurements and print Results

PerfMonitor.stop();

You do not need to explicitly call print method to print the results, stop() already covered that.

You can verify it by running:

...
  _setIndex(idx){
    PerfMonitor.toggle();
    PerfMonitor.start();
    this.setState({index:idx})
  }

  componentDidUpdate(){
    PerfMonitor.stop();
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Welcome to react-native {helloWorld()}</Text>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <Button title="click me to see profiling in console log" onPress={()=> this._setIndex(2)}/>
      </View>
    );
  }
...

Make sure Remote Debug JS is on, then you can see the results on chrome console.

like image 129
Kevin Li Avatar answered Mar 29 '26 10:03

Kevin Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!