Performance: Defining models
MEDIUM IMPACT
This affects server response time and initial page load speed by influencing how data is structured and retrieved.
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: { type: String, required: true, index: true }, age: { type: Number, min: 0 }, addressId: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } }); module.exports = mongoose.model('User', userSchema);
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: String, age: Number, address: { street: String, city: String, zip: String } }); module.exports = mongoose.model('User', userSchema);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested schema without indexes | N/A (server-side) | N/A | N/A | [X] Bad |
| Schema with indexed fields and references | N/A (server-side) | N/A | N/A | [OK] Good |