Performance Best Practices
This document covers performance optimizations to write efficient code. These rules identify patterns that cause unnecessary computational overhead, extra database queries, or memory inefficiency.
Python
Django - Access Foreign Keys Directly
Use ITEM.user_id rather than ITEM.user.id to prevent running an extra query. Accessing .user.id causes Django to fetch the entire related User object just to get the ID, when the foreign key ID is already available on the model.
INCORRECT - Extra query to fetch related object:
def get_user_id(item):
return item.user.idCORRECT - Use the foreign key directly:
def get_user_id(item):
return item.user_idSQLAlchemy - Use count() Instead of len(all())
Using QUERY.count() instead of len(QUERY.all()) sends less data to the client since the count is performed server-side. The len(all()) approach fetches all records into memory just to count them.
INCORRECT - Fetches all records into memory:
total = len(persons.all())CORRECT - Count performed server-side:
total = persons.count()SQLAlchemy - Batch Database Operations
Rather than adding one element at a time, use batch loading to improve performance. Looping db.session.add() increases session bookkeeping overhead and can trigger per-iteration SQL if autoflush is enabled (e.g., when a query runs during the loop).
INCORRECT - Adding one at a time in a loop:
for song in songs:
db.session.add(song)CORRECT - Batch add all at once:
db.session.add_all(songs)JavaScript/TypeScript
React - Define Styled Components at Module Level
By declaring a styled component inside the render method, you dynamically create a new component on every render. This forces React to discard and re-calculate that part of the DOM subtree on each render, leading to performance bottlenecks.
INCORRECT - Styled component declared inside function:
import styled from "styled-components";
function FunctionalComponent() {
const StyledDiv = styled.div`
color: blue;
`
return <StyledDiv />
}CORRECT - Styled component declared at module level:
import styled from "styled-components";
const StyledDiv = styled.div`
color: blue;
`
function FunctionalComponent() {
return <StyledDiv />
}Avoid Unnecessary Operations in Loops
Hoist expensive work (object allocations, RegExp compilation, function creation) out of loops.
INCORRECT - RegExp compiled on every iteration:
for (const line of lines) {
const match = line.match(new RegExp('\\d{4}-\\d{2}-\\d{2}'));
if (match) results.push(match[0]);
}CORRECT - Compile once, reuse in loop:
const datePattern = /\d{4}-\d{2}-\d{2}/;
for (const line of lines) {
const match = line.match(datePattern);
if (match) results.push(match[0]);
}For operations that require iterating, prefer built-in methods that short-circuit:
INCORRECT - Full iteration to find one item:
const found = items.filter(x => x.id === targetId)[0];CORRECT - Short-circuit on first match:
const found = items.find(x => x.id === targetId);