Hardhat deployment scripts in Blockchain / Solidity - Time & Space Complexity
When deploying smart contracts with Hardhat, it's important to understand how the deployment time changes as the number of contracts grows.
We want to know how the time to deploy scales when we add more contracts to deploy.
Analyze the time complexity of the following deployment script.
async function main() {
const contracts = ["Token", "Marketplace", "Auction"];
for (const name of contracts) {
const ContractFactory = await ethers.getContractFactory(name);
const contract = await ContractFactory.deploy();
await contract.deployed();
console.log(`${name} deployed at ${contract.address}`);
}
}
This script deploys multiple contracts one after another using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Deploying each contract inside the loop.
- How many times: Once for each contract in the list.
As the number of contracts to deploy increases, the total deployment time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 deployments |
| 10 | 10 deployments |
| 100 | 100 deployments |
Pattern observation: Doubling the number of contracts roughly doubles the total deployment time.
Time Complexity: O(n)
This means the deployment time grows linearly with the number of contracts you deploy.
[X] Wrong: "Deploying multiple contracts in a loop happens instantly or all at once."
[OK] Correct: Each deployment is a separate transaction that takes time, so the total time adds up with each contract.
Understanding how deployment time scales helps you plan and write efficient deployment scripts, a useful skill when working on real blockchain projects.
"What if we deployed all contracts in parallel instead of one after another? How would the time complexity change?"