I asked Github Copilot Models to “refactor” the code. Yes, that’s it. That’s all I said. The code was basic, and unrefactored for fetching users from an external API.
Note: Running models again and again had different results, so these comparison codes are the first try.
## Intentionally Unoptimized Code
An intentionally unoptimized and unrefactored React component that fetches and displays a list of users. It has unnecessary re-renders, inefficient state updates, and other issues.
The code is placed in a “.tsx“ file.
```typescript
import React, { useState, useEffect } from 'react';
const UserList = () => {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [search, setSearch] = useState('');
const [filteredUsers, setFilteredUsers] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data => {
setUsers(data);
setLoading(false);
})
.catch(() => setLoading(false));
}, []);
useEffect(() => {
const filtered = users.filter(user =>
user.name.toLowerCase().includes(search.toLowerCase())
);
setFilteredUsers(filtered);
}, [search, users]);
return (
);
};
export default UserList;
```
Explanation Received:
Key improvements:
1. **TypeScript support** - Added proper type definitions
2. **Modern async/await** - Replaced promise chains with cleaner syntax
3. **Better error handling** - Added error state and display
4. **Performance optimization** - Replaced the second useEffect with useMemo
5. **Accessibility** - Added aria-label and semantic class names
6. **User experience** - Added "no results" state when search returns nothing
7. **Code organization** - Extracted the onChange handler for better readability
8. **Clean structure** - Better organization of component sections
---
## **Final Thoughts**
3.5 and 3.7 read that it’s a “.tsx“ file and made arrangements accordingly, introduced error states, and provided detailed explanations. 3.5 felt detailed whereas 3.7 was concise.
Whereas 4o and o1 were refactored in JavaScript. While 4o provided a bit of explanation, o1 was just straight to the point “Here’s your code sir…“.
…..
That’s it, folks! Hope it was a good read 🚀