How do I issue an alert and restart the application if the function is running too long?

There is a calculator that works through expo.There is a function that performs counting. However, if you enter a very large expression with a factorial there, for example, the calculator will hang tightly, as in the case, for example, if the counting function works for more than 3-5s and does not produce a result, issue an error and restart / throw it out of the application

 const getResult = () => {


        return (<TextInput
                onContentSizeChange={x => {
                    //console.log(x)
                }}
                editable={true}
                style={styles.expression}
                multiline={true}
                keyboardType={"number-pad"}
                textAlign="right"
                textAlignVertical="top"

            >{highlight(expr)}</TextInput>

        )

    }

    const Display = () =>
        <View style={styles.display}>
            {}

                {getResult()}

            {}
        </View>

Here is the function itself that returns the result
I tried to do
Put

var t0 = performance.now();

before GetResult()
and




     var t1 = performance.now();
    if (t1-t0>0.01) {

        BackHandler.exitApp();

    }

    console.log("Call to doSomething took " + ( t1-t0) + " milliseconds.")

after Display(), however, when the application freezes, it does not return the end time of the function, so exiting the application does not work, please help fix it.