In terms of performance, the Convex documentation and knowledge sources do not provide specific information on the performance difference between using v.union() with v.literal() versus v.number().
However, in a Discord support thread (https://discord.com/channels/1019350475847499849/1272890661636673666), a Convex staff member mentioned that changing schema to use numbers instead of strings could potentially reduce database bandwidth. This suggests that using v.number() might be more efficient in terms of bandwidth usage.
That being said, it's important to consider the readability and maintainability of your code. Using v.literal() with meaningful string values can make your code more self-explanatory, while using v.number() with arbitrary numeric codes might require additional documentation and could potentially lead to confusion.
Here's how you might define your schema using v.number():
import { v } from "convex/values";
import { defineSchema, defineTable } from "convex/server";
export default defineSchema({
recipes: defineTable({
name: v.string(),
course: v.union(
v.number(0),
v.number(1),
v.number(2)
),
ingredients: v.array(v.string()),
steps: v.array(v.string())
}).index("by_course", ["course"]),
});
In this case, you would need to document that 0 represents 'appetizer', 1 represents 'main', and 2 represents 'dessert'.
In conclusion, while using v.number() might potentially offer some performance benefits, you should also consider the trade-off in terms of code readability and maintainability.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: