Which code is more efficient?

I'm creating an ERC1155 contract and want to set URI for each token, so I'm using ERC1155URIStorage storage as well. However, I'm thinking which method below would be most efficient for adding _setURI in the mintBatch method.

  1. Add loop after calling _mintBatch.
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol";
// other imports
// other code
function mintBatch(
    address to,
    uint256[] memory ids,
    uint256[] memory amounts,
    string[] memory cids
) public onlyRole(MINTER_ROLE) {
    _mintBatch(to, ids, amounts, "");

    for (uint256 i = 0; i < ids.length; i++) {
        _setURI(ids[i], cids[i]);
    }
}
  1. Override _mintBatch and insert _setURI(ids[i], cids[i]) in the existing loop.
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol";
// other imports
// other code
function mintBatch(
    address to,
    uint256[] memory ids,
    uint256[] memory amounts,
    string[] memory cids
) public onlyRole(MINTER_ROLE) {
    _mintBatch(to, ids, amounts, "");
}

function _mintBatch(
    address to,
    uint256[] memory ids,
    uint256[] memory amounts,
    bytes memory data
) internal virtual override {
    require(to != address(0), "ERC1155: mint to the zero address");
    require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

    address operator = _msgSender();

    _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);

    for (uint256 i = 0; i < ids.length; i++) {
        _balances[ids[i]][to] += amounts[i];
        _setURI(ids[i], cids[i]);
    }

    emit TransferBatch(operator, address(0), to, ids, amounts);
    _afterTokenTransfer(operator, address(0), to, ids, amounts, data);
    _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
}

As for the first, I'm not sure if a second loop would be efficient. But I just feel like the second option will increase my contract size unnecessarily for just one line of code.

Please feel free to suggest better solutions as well!

When in doubt, you can always use Hardhat with the hardhat-gas-reporter plugin and let the EVM tell you the answer :slight_smile:

I see. Will do that, thank you! :slight_smile: