0
0
Reactframework~8 mins

Form submission handling in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Form submission handling
MEDIUM IMPACT
This affects how quickly the page responds to user input and how fast the form data is processed and sent, impacting interaction responsiveness and perceived speed.
Handling form submission in React
React
function MyForm() {
  const handleSubmit = async (e) => {
    e.preventDefault();
    await new Promise(resolve => setTimeout(resolve, 0)); // yield to browser
    alert('Form submitted');
  };
  return (
    <form onSubmit={handleSubmit}>
      <button type="submit">Submit</button>
    </form>
  );
}
Using async and yielding to the browser prevents blocking, keeping UI responsive during submission.
📈 Performance GainNon-blocking submission, improves INP by avoiding main thread blocking.
Handling form submission in React
React
function MyForm() {
  return (
    <form onSubmit={(e) => {
      e.preventDefault();
      // heavy synchronous processing
      for(let i=0; i<1000000000; i++) {}
      alert('Form submitted');
    }}>
      <button type="submit">Submit</button>
    </form>
  );
}
Heavy synchronous processing blocks the main thread, causing input lag and delayed UI updates.
📉 Performance CostBlocks rendering for 200+ ms depending on device, causing poor INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy processing on submitMinimal0 immediate, but blocksHigh due to blocking[X] Bad
Async lightweight submit handlerMinimal0 immediateLow[OK] Good
Default form submit with page reloadFull page reloadMany reflowsVery high[X] Bad
Prevent default + async submitMinimal0 immediateLow[OK] Good
Rendering Pipeline
Form submission handling affects the main thread during event handling, impacting the browser's ability to update styles, layout, and paint promptly.
Event Handling
Style Calculation
Layout
Paint
⚠️ BottleneckEvent Handling blocking main thread delays subsequent rendering stages.
Core Web Vital Affected
INP
This affects how quickly the page responds to user input and how fast the form data is processed and sent, impacting interaction responsiveness and perceived speed.
Optimization Tips
1Avoid heavy synchronous code in form submit handlers to keep UI responsive.
2Always call e.preventDefault() to prevent full page reloads on submit.
3Use async operations to handle form data submission without blocking the main thread.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with heavy synchronous work inside a form submit handler?
AIt blocks the main thread, causing input lag and delayed UI updates.
BIt increases the bundle size significantly.
CIt causes the browser to skip painting the page.
DIt automatically triggers a full page reload.
DevTools: Performance
How to check: Record a performance profile while submitting the form. Look for long tasks blocking the main thread during the submit event.
What to look for: Check for long 'Event Handler' tasks and main thread blocking time; shorter tasks indicate better performance.