Performance: Role-based access patterns
This affects page load speed and interaction responsiveness by controlling which components and data are loaded based on user roles.
Jump into concepts and practice - no test required
export default function Page({ user }) { if (user.role === 'admin') { return <AdminComponent /> } else if (user.role === 'editor') { return <EditorComponent /> } else { return <CommonComponent /> } }
export default function Page({ user }) { return ( <div> <button>Common Action</button> <button style={{ display: user.role === 'admin' ? 'block' : 'none' }}>Admin Action</button> <button style={{ display: user.role === 'editor' ? 'block' : 'none' }}>Editor Action</button> </div> ) }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Render all role UI elements with conditional display | High (many hidden nodes) | Multiple reflows due to style changes | High paint cost for hidden elements | [X] Bad |
| Render only role-specific components | Low (minimal nodes) | Single reflow | Low paint cost | [OK] Good |
| Fetch all role data regardless of user | N/A | N/A | Blocks rendering longer | [X] Bad |
| Fetch only role-specific data | N/A | N/A | Faster rendering, smaller payload | [OK] Good |
function Dashboard({ session }) {
if (session.user.role === 'admin') {
return <div>Admin Panel</div>;
} else if (session.user.role === 'editor') {
return <div>Editor Workspace</div>;
} else {
return <div>Access Denied</div>;
}
}function Page({ session }) {
if (session.user.role = 'admin') {
return <div>Admin Access</div>;
}
return <div>No Access</div>;
}