1

I have the following function within my contract.

function getAllLandForSale() external view returns(uint256[]){

        // Get the total number of properties created
        uint256 totalNumberOfProperties = baseContract.getLandArrayLength();

        uint[] memory propertiesForSaleArray = new uint[](totalNumberOfProperties);

        for(uint i = 0; i < totalNumberOfProperties; ++i) {

            address currentOwner = baseContract.getLandOwner(i);

            if(currentOwner == address(0)){

                propertiesForSaleArray[i] = i;

            }

        }

        return propertiesForSaleArray;

    }

Lets say the totalNumberOfProperties variable is 100,000. Obviously the function is looping through each one and checking the currentOwner variable.

Would it be faster to do it like this within the contract or filter through the array on the front-end using Javascript. I come from a PHP, MYSQL background so obviously this would be pretty easy using DB queries but I don't want to take that route.

The reason being these results help to populate a paginated listing page and I would like the user to be able to filter through the results.

Thanks

1

You should definitely do this off chain by getting the whole array and filtering off-chain.

Even though a view function does not modify state, it is still subject to gas usage and hence out of gas if it uses too much computation. So if your array gets too big, it will not be possible to execute this function anymore.

I was unable to find the documentation on that

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.