# What is the additional gas cost of the OpenZeppelin ReentrancyGuard?

**URL:** <https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750>\
**Category:** Contracts\
**Created:** [November 14, 2019, 1:39am UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750 "2019-11-14T01:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 14, 2019, 1:39am UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/1 "2019-11-14T01:39:36Z")

</div>

Asked by [idevelopthereforeiam](https://www.reddit.com/user/idevelopthereforeiam/) on [r/ethdev](https://www.reddit.com/r/ethdev) as a comment on a link to [Reentrancy After Istanbul](https://forum.openzeppelin.com/t/reentrancy-after-istanbul/1742)

> [https://www.reddit.com/r/ethdev/comments/dvg8hq/reentrancy\_is\_becoming\_unavoidable\_in/f7e9yd4](https://www.reddit.com/r/ethdev/comments/dvg8hq/reentrancy_is_becoming_unavoidable_in/f7e9yd4)  
> What is the additional gas cost of the OpenZeppelin ReentrancyGuard?

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 14, 2019, 2:57am UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/2 "2019-11-14T02:57:03Z")

</div>

I tried using the `nonReentrant` modifier on a test contract on Ropsten and got a 7,447 gas overhead.

* * *

Calling a function (`doStuff`)  
Gas Used by Transaction: 21,208  
[https://ropsten.etherscan.io/tx/0x722f698f708fcdd1b435d5b12f845b13cf61794deb2b8c2e7e9aae4ab52e1ba5](https://ropsten.etherscan.io/tx/0x722f698f708fcdd1b435d5b12f845b13cf61794deb2b8c2e7e9aae4ab52e1ba5)

Calling a function (`doStuffNonReentrant`) with the `nonReentrant` modifier  
Gas Used by Transaction: 28,655  
[https://ropsten.etherscan.io/tx/0x6d8cd74637ae08706691a95c3f359b39353a4421672ac46089ff503861745225](https://ropsten.etherscan.io/tx/0x6d8cd74637ae08706691a95c3f359b39353a4421672ac46089ff503861745225)

28,655 - 21,208 = 7,447 gas overhead

## Test.sol

```auto
pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/utils/ReentrancyGuard.sol";

contract Test is ReentrancyGuard {

    function doStuff() public {
    }

    function doStuffNonReentrant() public nonReentrant {
    }
}

```

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [November 14, 2019, 3:15pm UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/3 "2019-11-14T15:15:25Z")

</div>

@nventuro and @DericEcourcy recently revamped `ReentrancyGuard` to be cheaper when the upcoming net gas metering lands.

They may have some insights into what the gas costs will be with this change.

> <https://github.com/OpenZeppelin/openzeppelin-contracts/pull/1992>

---

<div class="post-metadata">

**Author:** ![nventuro](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/nventuro/32/8_2.png) [@nventuro](https://forum.openzeppelin.com/u/nventuro)\
**Post date:** [November 14, 2019, 10:49pm UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/4 "2019-11-14T22:49:01Z")

</div>

> [@frangio](#):
>
> @nventuro and @DericEcourcy recently revamped `ReentrancyGuard` to be cheaper when the upcoming net gas metering lands.
> 
> They may have some insights into what the gas costs will be with this change.

The overhead should be 4 `SLOAD`, 5 if the compiler is not smart. `SLOAD`s are being repriced from 200 to 800 however, so I'd expect ~3.5k gas.

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 15, 2019, 12:42am UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/5 "2019-11-15T00:42:25Z")

</div>

Hi @nventuro,

Is there a better way to test the overhead, as I got a larger figure than ~3.5k?

---

<div class="post-metadata">

**Author:** ![aviekakkar](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/aviekakkar/32/3805_2.png) [@aviekakkar](https://forum.openzeppelin.com/u/aviekakkar)\
**Post date:** [January 2, 2022, 6:12pm UTC](https://forum.openzeppelin.com/t/what-is-the-additional-gas-cost-of-the-openzeppelin-reentrancyguard/1750/6 "2022-01-02T18:12:06Z")

</div>

did you find an alternative here?
