0
0
Blockchain / Solidityprogramming~5 mins

Hardhat deployment scripts in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Hardhat deployment scripts
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of contracts to deploy increases, the total deployment time grows roughly in direct proportion.

Input Size (n)Approx. Operations
33 deployments
1010 deployments
100100 deployments

Pattern observation: Doubling the number of contracts roughly doubles the total deployment time.

Final Time Complexity

Time Complexity: O(n)

This means the deployment time grows linearly with the number of contracts you deploy.

Common Mistake

[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.

Interview Connect

Understanding how deployment time scales helps you plan and write efficient deployment scripts, a useful skill when working on real blockchain projects.

Self-Check

"What if we deployed all contracts in parallel instead of one after another? How would the time complexity change?"