Custom functions in rules in Firebase - Time & Space Complexity
When using custom functions in Firebase security rules, it's important to know how the number of times these functions run affects performance.
We want to understand how the work grows as more data or checks happen.
Analyze the time complexity of this Firebase rule using a custom function.
function isOwner(userId) {
return request.auth.uid == userId;
}
match /documents/{docId} {
allow read: if isOwner(resource.data.ownerId);
}
This rule checks if the user requesting data is the owner by calling a custom function.
Look at what happens each time a request is checked.
- Primary operation: The custom function
isOwneris called once per request. - How many times: Exactly once for each document read request.
As the number of read requests grows, the number of function calls grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 calls to isOwner |
| 100 | 100 calls to isOwner |
| 1000 | 1000 calls to isOwner |
Pattern observation: The number of function calls grows directly with the number of requests.
Time Complexity: O(n)
This means the work grows in a straight line with the number of requests checked.
[X] Wrong: "The custom function runs only once no matter how many requests come in."
[OK] Correct: Each request triggers the function separately, so the total work grows with requests.
Understanding how custom functions scale helps you design rules that stay fast as your app grows.
What if the custom function called another function inside it? How would that affect the time complexity?
Practice
custom functions in Firebase security rules?Solution
Step 1: Understand the purpose of custom functions
Custom functions in Firebase rules are designed to simplify and organize security logic by reusing common checks.Step 2: Compare options to the purpose
Only They make rules easier to read and reuse common checks correctly states that custom functions make rules easier to read and reuse common checks. Other options describe unrelated or incorrect benefits.Final Answer:
They make rules easier to read and reuse common checks -> Option AQuick Check:
Custom functions improve readability and reuse = A [OK]
- Thinking custom functions speed up queries
- Believing they fix security automatically
- Confusing them with authentication features
isOwner in Firebase rules?Solution
Step 1: Recall Firebase rules function syntax
Firebase rules use the syntax:function name() { return expression; }to define custom functions.Step 2: Identify the correct syntax
function isOwner() { return request.auth.uid == resource.data.owner; }matches the correct syntax.isOwner() { return request.auth.uid == resource.data.owner; }omits the requiredfunctionkeyword. Options B and C use invalid syntax.Final Answer:
function isOwner() { return request.auth.uid == resource.data.owner; } -> Option CQuick Check:
Firebase custom function syntax uses function name() { return ... } = A [OK]
- Omitting the 'function' keyword
- Using arrow functions which are not supported
- Using '=' instead of function body block
function isAdmin() { return request.auth.token.admin == true; }
allow write: if isAdmin();What happens if
request.auth.token.admin is false?Solution
Step 1: Understand the function logic
The functionisAdmin()returns true only ifrequest.auth.token.adminis true.Step 2: Evaluate the rule condition
The write permission is allowed only ifisAdmin()returns true. Ifrequest.auth.token.adminis false, the function returns false, so write is denied.Final Answer:
Write access is denied -> Option AQuick Check:
isAdmin false means no write access = A [OK]
- Assuming false admin still grants access
- Thinking rule throws error on false
- Confusing authentication with admin check
function isOwner() {
request.auth.uid == resource.data.owner;
}
allow read: if isOwner();Solution
Step 1: Check function body for return
The function compares values but does not return the result, so it returns null by default.Step 2: Understand impact on rule
Without a return,isOwner()returns null (falsey), so the rule will deny access unexpectedly.Final Answer:
Missing return statement inside the function -> Option DQuick Check:
Functions must return values explicitly = B [OK]
- Forgetting to add 'return' before expression
- Assuming last line auto-returns value
- Misplacing 'allow' statements
canEdit() that allows editing if the user is either the owner or has an admin role. Which of the following is the correct way to write this function in Firebase rules?Solution
Step 1: Understand the logic needed
The function should return true if the user is owner OR has admin role.Step 2: Analyze each option
function canEdit() { return request.auth.uid == resource.data.owner || request.auth.token.admin == true; }correctly usesfunctionkeyword, logical OR (||) and explicit admin comparison.canEdit() { if (request.auth.uid == resource.data.owner) return true; else return false; }only checks owner.canEdit() { return request.auth.uid == resource.data.owner || request.auth.token.admin == true; }andcanEdit() { return request.auth.uid == resource.data.owner || request.auth.token.admin; }omit requiredfunctionkeyword.Final Answer:
function canEdit() { return request.auth.uid == resource.data.owner || request.auth.token.admin == true; } -> Option BQuick Check:
function keyword + OR + admin == true = B [OK]
- Using AND instead of OR for conditions
- Not explicitly comparing admin to true
- Ignoring admin role in logic
Ready to prove it?
Pass the test and this pattern moves unknown
