I have this contract:

pragma solidity 0.5;
pragma experimental ABIEncoderV2;
contract TestStruct {

      struct User {
        string name;
        uint256 age;

    mapping (bytes32 => User) users;

    function addManyUsers (User [] memory _users) public {

        for (uint i = 0; i < _users.length; i++) {

           bytes32 hash = keccak256(abi.encode(_users[i].name));
           users[hash] = _users[i];


    function addOneUser (User memory _user) public {

         bytes32 hash = keccak256(abi.encode(_user.name));

           users[hash] = _user;


    function getUser (string memory username) public view returns (User memory) {

        bytes32 hash = keccak256(abi.encode(username));

        return users[hash];

Let's say we have a list of 100 users,

which is more faster in terms of execution: insert one by one (using addOneUser) or insert all the one once (using addManyUsers) ?

1 Answer 1


It would be faster (and cheaper) to use addManyUsers().

In terms of speed, the blockchain processes transactions of all complexity at the same time (each block). What should be of concern to you is the cost of gas and the number of transactions being sent.

When determining the best option from a choice of transactions, you should consider the total gas cost of these transactions. It is good to keep in mind that each opcode in a transaction has a certain cost associated with it.

The other thing to think about is number of transactions. In general, more transactions will cost more, as each new transaction has a large up-front gas cost. Because of this, it would be much cheaper to perform addManyUsers() over 2-3 transactions (doing all at once will likely cause you to hit the block gas limit), as opposed to addOneUser() 100 times, as the transaction cost for 100 transactions will be greater than that of 2-3 transactions.

  • 1
    Thanks for your nice answer, now what about best practices ? which one is better?
    – maroodb
    Jul 2, 2019 at 8:07
  • 1
    addManyUsers() Jul 2, 2019 at 12:08
  • Hello again, when trying to send many users I get ws disconnected and when using rpc i geth this errror: "Error: Returned error: oversized data "
    – maroodb
    Jul 9, 2019 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.