HTML Preparation code:
x
 
1
<!doctype html>
2
<html>
3
  <head>
4
    <title>This is the title of the webpage!</title>
5
  </head>
6
  <body>
7
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
8
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
9
    <div id="root"></div> 
10
  </body>
11
</html>
12
Script Preparation code:
 
function ComponentWithOuterFunctionUseCallback() {
  const [state, setState] = React.useState();
  function functionToTest(evt) {
    evt.preventDefault();
    const period = "20231105010814-1";
    const periodRegex = /^\d{14}-\d+$/;
    if (!periodRegex.test(period)) {
        throw new Error("Invalid period format.");
    }
    const [start, end] = period.split("-").map(Number);
    if (isNaN(start) || isNaN(end) || start >= end) {
        throw new Error("Invalid start or end value.");
    }
    const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
    setState(randomNumber);
  }
  const testFunction = React.useCallback(functionToTest, []);
  return React.createElement('button', {onClick: testFunction}, 'Test click');
}
function ComponentWithUseCallback() {
  const [state, setState] = React.useState();
  const testFunction = React.useCallback(
    evt => {
      evt.preventDefault(); 
      const period = "20231105010814-1";
      const periodRegex = /^\d{14}-\d+$/;
      if (!periodRegex.test(period)) {
          throw new Error("Invalid period format.");
      }
      const [start, end] = period.split("-").map(Number);
      if (isNaN(start) || isNaN(end) || start >= end) {
          throw new Error("Invalid start or end value.");
      }
      const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
      setState(randomNumber);
    }, 
    []
  );
  return React.createElement('button', {onClick: testFunction}, 'Test click');
}
function ComponentWithInlineFunction() {
  const [state, setState] = React.useState();
  function testFunction(evt) {
    evt.preventDefault();
    const period = "20231105010814-1";
    const periodRegex = /^\d{14}-\d+$/;
    if (!periodRegex.test(period)) {
        throw new Error("Invalid period format.");
    }
    const [start, end] = period.split("-").map(Number);
    if (isNaN(start) || isNaN(end) || start >= end) {
        throw new Error("Invalid start or end value.");
    }
    const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
    setState(randomNumber);
  }
  return React.createElement('button', {onClick: testFunction}, 'Test click');
}
function ComponentWithArrowFunction() {
  const [state, setState] = React.useState();
  const testFunction = (evt) => {
    evt.preventDefault();
    const period = "20231105010814-1";
    const periodRegex = /^\d{14}-\d+$/;
    if (!periodRegex.test(period)) {
        throw new Error("Invalid period format.");
    }
    const [start, end] = period.split("-").map(Number);
    if (isNaN(start) || isNaN(end) || start >= end) {
        throw new Error("Invalid start or end value.");
    }
    const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
    setState(randomNumber);
  }
  return React.createElement('button', {onClick: testFunction}, 'Test click');
}
const testFunctionInlineOutside = function(evt, setState) {
  return evt.preventDefault();
  const period = "20231105010814-1";
  const periodRegex = /^\d{14}-\d+$/;
  if (!periodRegex.test(period)) {
    throw new Error("Invalid period format.");
  }
  const [start, end] = period.split("-").map(Number);
  if (isNaN(start) || isNaN(end) || start >= end) {
    throw new Error("Invalid start or end value.");
  }
  const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
  setState(randomNumber);
}
function ComponentWithInlineFunctionOutside() {
  const [state, setState] = React.useState();
  return React.createElement('button', {onClick: (evt) => testFunctionArrowOutside(evt, setState)}, 'Test click');
}
const testFunctionArrowOutside = (evt, setState) => {
  evt.preventDefault();
  const period = "20231105010814-1";
  const periodRegex = /^\d{14}-\d+$/;
  if (!periodRegex.test(period)) {
    throw new Error("Invalid period format.");
  }
  const [start, end] = period.split("-").map(Number);
  if (isNaN(start) || isNaN(end) || start >= end) {
    throw new Error("Invalid start or end value.");
  }
  const randomNumber = Math.floor(Math.random() * (end - start + 1)) + start;
  setState(randomNumber);
}
function ComponentWithArrowFunctionOutside() {
  const [state, setState] = React.useState();
  return React.createElement('button', {onClick: (evt) => testFunctionArrowOutside(evt, setState)}, 'Test click');
}
Tests:
  • ComponentWithOuterFunctionUseCallback

     
    ReactDOM.render(React.createElement(ComponentWithOuterFunctionUseCallback), document.getElementById('root'))
  • ComponentWithUseCallback

     
    ReactDOM.render(React.createElement(ComponentWithUseCallback), document.getElementById('root'))
  • ComponentWithInlineFunction

     
    ReactDOM.render(React.createElement(ComponentWithInlineFunction), document.getElementById('root'))
  • ComponentWithArrowFunction

     
    ReactDOM.render(React.createElement(ComponentWithArrowFunction), document.getElementById('root'))
  • ComponentWithInlineFunctionOutside

     
    ReactDOM.render(React.createElement(ComponentWithInlineFunctionOutside), document.getElementById('root'))
  • ComponentWithArrowFunctionOutside

     
    ReactDOM.render(React.createElement(ComponentWithArrowFunctionOutside), document.getElementById('root'))
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    ComponentWithOuterFunctionUseCallback
    ComponentWithUseCallback
    ComponentWithInlineFunction
    ComponentWithArrowFunction
    ComponentWithInlineFunctionOutside
    ComponentWithArrowFunctionOutside

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
ComponentWithOuterFunctionUseCallback 395204.8 Ops/sec
ComponentWithUseCallback 407350.7 Ops/sec
ComponentWithInlineFunction 355689.8 Ops/sec
ComponentWithArrowFunction 353560.9 Ops/sec
ComponentWithInlineFunctionOutside 355694.2 Ops/sec
ComponentWithArrowFunctionOutside 360006.7 Ops/sec