CINXE.COM

Gas Saving Techniques in Solidity - DEV Community

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Gas Saving Techniques in Solidity - DEV Community</title> <meta name="last-updated" content="2025-01-05 23:42:21 UTC"> <meta name="user-signed-in" content="false"> <meta name="head-cached-at" content="1736120541"> <meta name="environment" content="production"> <link rel="stylesheet" href="https://assets.dev.to/assets/minimal-1be4230b8ec9f13239b8d6cfe8d9065ceefcbb79eac80b47e4779f5ba094b63a.css" media="all" id="main-minimal-stylesheet" /> <link rel="stylesheet" href="https://assets.dev.to/assets/views-252792a124dd89807543985e593596806e3983f484a61770ac9da06c26e5aa9e.css" media="all" id="main-views-stylesheet" /> <link rel="stylesheet" href="https://assets.dev.to/assets/crayons-c04ad8a7afa1453c505f39a8d02e5df1ecce688d9144c4a2f6b9aa8776ed9a09.css" media="all" id="main-crayons-stylesheet" /> <script src="https://assets.dev.to/assets/base-e0fa4f0ebd0f1ec157e6b07c08f9222f8c0cca3d10cfde4cd8c951b43985b10b.js" defer="defer"></script> <script src="https://assets.dev.to/assets/application-147cebefc5c4cddde055e8f5eb0055e811469b08405170e2411fbd7944b5ac04.js" defer="defer"></script> <script src="https://assets.dev.to/assets/baseInitializers-bc498cfd7bb7d2a2da59d68d0b2055cc2dd26fee3669ab88edbb396d37bc3369.js" defer="defer"></script> <script src="https://assets.dev.to/assets/baseTracking-b6bf73e5ee66633e151e7d5b7c6bbccedfa4c59e3615be97b98c4c0f543ddae7.js" defer="defer"></script> <meta name="search-script" content="https://assets.dev.to/assets/Search-cc5e8a352578866203771def747f37c3ec6a0869de0458328e0fcba3d5d2fceb.js"> <link rel="canonical" href="https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c" /> <meta name="description" content="Gas is a unit of computational measure in Solidity. This is the amount that we pay to interact and... Tagged with solidity, gas, blockchain, calldata."> <meta name="keywords" content="solidity, gas, blockchain, calldata, software, coding, development, engineering, inclusive, community"> <meta property="og:type" content="article" /> <meta property="og:url" content="https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c" /> <meta property="og:title" content="Gas Saving Techniques in Solidity" /> <meta property="og:description" content="Gas is a unit of computational measure in Solidity. This is the amount that we pay to interact and..." /> <meta property="og:site_name" content="DEV Community" /> <meta name="twitter:site" content="@thepracticaldev"> <meta name="twitter:creator" content="@Jamiescript"> <meta name="twitter:title" content="Gas Saving Techniques in Solidity"> <meta name="twitter:description" content="Gas is a unit of computational measure in Solidity. This is the amount that we pay to interact and..."> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:widgets:new-embed-design" content="on"> <meta name="robots" content="max-snippet:-1, max-image-preview:large, max-video-preview:-1"> <meta property="og:image" content="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otvb2z646ytpt1hl2rv.jpg" /> <meta name="twitter:image:src" content="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otvb2z646ytpt1hl2rv.jpg"> <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"> <link rel="icon" type="image/x-icon" href="https://media2.dev.to/dynamic/image/width=32,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" /> <link rel="apple-touch-icon" href="https://media2.dev.to/dynamic/image/width=180,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png"> <link rel="apple-touch-icon" sizes="152x152" href="https://media2.dev.to/dynamic/image/width=152,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png"> <link rel="apple-touch-icon" sizes="180x180" href="https://media2.dev.to/dynamic/image/width=180,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png"> <link rel="apple-touch-icon" sizes="167x167" href="https://media2.dev.to/dynamic/image/width=167,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png"> <link href="https://media2.dev.to/dynamic/image/width=192,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" rel="icon" sizes="192x192" /> <link href="https://media2.dev.to/dynamic/image/width=128,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" rel="icon" sizes="128x128" /> <meta name="apple-mobile-web-app-title" content="dev.to"> <meta name="application-name" content="dev.to"> <meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)"> <meta name="theme-color" content="#000000" media="(prefers-color-scheme: dark)"> <link rel="search" href="https://dev.to/open-search.xml" type="application/opensearchdescription+xml" title="DEV Community" /> <meta property="forem:name" content="DEV Community" /> <meta property="forem:logo" content="https://media2.dev.to/dynamic/image/width=512,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" /> <meta property="forem:domain" content="dev.to" /> </head> <body class="sans-serif-article-body default-header" data-user-status="logged-out" data-community-name="DEV Community" data-subscription-icon="https://assets.dev.to/assets/subscription-icon-805dfa7ac7dd660f07ed8d654877270825b07a92a03841aa99a1093bd00431b2.png" data-locale="en" data-honeybadger-key="hbp_nqu4Y66HuEKlD6YRGssZuRQnPOjDm50J8Zkr" data-deployed-at="2025-01-03T19:13:54Z" data-latest-commit-id="9c1b4a24a79f182ef5c51951e780280792cb0d08" data-ga-tracking="UA-71991109-1" data-cookie-banner-user-context="logged_out_only" data-cookie-banner-platform-context="off" data-algolia-id="PRSOBFP46H" data-algolia-search-key="9aa7d31610cba78851c9b1f63776a9dd" data-algolia-display="true" data-ga4-tracking-id="G-TYEM8Y3JN3"> <link rel="stylesheet" href="https://assets.dev.to/assets/minimal-1be4230b8ec9f13239b8d6cfe8d9065ceefcbb79eac80b47e4779f5ba094b63a.css" media="all" id="secondary-minimal-stylesheet" /> <link rel="stylesheet" href="https://assets.dev.to/assets/views-252792a124dd89807543985e593596806e3983f484a61770ac9da06c26e5aa9e.css" media="all" id="secondary-views-stylesheet" /> <link rel="stylesheet" href="https://assets.dev.to/assets/crayons-c04ad8a7afa1453c505f39a8d02e5df1ecce688d9144c4a2f6b9aa8776ed9a09.css" media="all" id="secondary-crayons-stylesheet" /> <div id="body-styles"> <style> :root { --accent-brand-lighter-rgb: 80, 99, 301; --accent-brand-rgb: 59, 73, 223; --accent-brand-darker-rgb: 47, 58, 178; } </style> </div> <div id="audiocontent" data-podcast=""> </div> <div class="navigation-progress" id="navigation-progress"></div> <header id="topbar" class="crayons-header topbar print-hidden"> <span id="route-change-target" tabindex="-1"></span> <a href="#main-content" class="skip-content-link">Skip to content</a> <div class="crayons-header__container"> <span class="inline-block m:hidden "> <button class="c-btn c-btn--icon-alone js-hamburger-trigger mx-2"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-labelledby="afd72zcww3lusj4xxpkgoze0kbt74kja" class="crayons-icon"><title id="afd72zcww3lusj4xxpkgoze0kbt74kja">Navigation menu</title> <path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"></path> </svg> </button> </span> <a href="/" class="site-logo" aria-label="DEV Community Home"> <img class="site-logo__img" src="https://media2.dev.to/dynamic/image/quality=100/https://dev-to-uploads.s3.amazonaws.com/uploads/logos/resized_logo_UQww2soKuUsjaOGNB38o.png" style="aspect-ratio: 10 / 8" alt="DEV Community"> </a> <div class="crayons-header--search js-search-form" id="header-search"> <form accept-charset="UTF-8" method="get" action="/search" role="search"> <div class="crayons-fields crayons-fields--horizontal"> <div class="crayons-field flex-1 relative"> <input id="search-input" class="crayons-header--search-input crayons-textfield js-search-input" type="text" id="nav-search" name="q" placeholder="Find related posts..." autocomplete="off" /> <button type="submit" aria-label="Search" class="c-btn c-btn--icon-alone absolute inset-px right-auto mt-0 py-0"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-labelledby="aaoprnyd1o6c2689ssctaniqyelpfko2" aria-hidden="true" class="crayons-icon"><title id="aaoprnyd1o6c2689ssctaniqyelpfko2">Search</title> <path d="M18.031 16.617l4.283 4.282-1.415 1.415-4.282-4.283A8.96 8.96 0 0111 20c-4.968 0-9-4.032-9-9s4.032-9 9-9 9 4.032 9 9a8.96 8.96 0 01-1.969 5.617zm-2.006-.742A6.977 6.977 0 0018 11c0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7a6.977 6.977 0 004.875-1.975l.15-.15z"></path> </svg> </button> <a class="crayons-header--search-brand-indicator" href="https://www.algolia.com/developers/?utm_source=devto&utm_medium=referral" target="_blank" rel="noopener noreferrer"> Powered by <svg xmlns="http://www.w3.org/2000/svg" id="Layer_1" width="24" height="24" viewBox="0 0 500 500.34" role="img" aria-labelledby="aqm6wxqxtt9ls1d7r1sfk8q5m42jr1fc" aria-hidden="true" class="crayons-icon"><title id="aqm6wxqxtt9ls1d7r1sfk8q5m42jr1fc">Search</title> <defs></defs><path class="cls-1" d="M250,0C113.38,0,2,110.16,.03,246.32c-2,138.29,110.19,252.87,248.49,253.67,42.71,.25,83.85-10.2,120.38-30.05,3.56-1.93,4.11-6.83,1.08-9.52l-23.39-20.74c-4.75-4.22-11.52-5.41-17.37-2.92-25.5,10.85-53.21,16.39-81.76,16.04-111.75-1.37-202.04-94.35-200.26-206.1,1.76-110.33,92.06-199.55,202.8-199.55h202.83V407.68l-115.08-102.25c-3.72-3.31-9.43-2.66-12.43,1.31-18.47,24.46-48.56,39.67-81.98,37.36-46.36-3.2-83.92-40.52-87.4-86.86-4.15-55.28,39.65-101.58,94.07-101.58,49.21,0,89.74,37.88,93.97,86.01,.38,4.28,2.31,8.28,5.53,11.13l29.97,26.57c3.4,3.01,8.8,1.17,9.63-3.3,2.16-11.55,2.92-23.6,2.07-35.95-4.83-70.39-61.84-127.01-132.26-131.35-80.73-4.98-148.23,58.18-150.37,137.35-2.09,77.15,61.12,143.66,138.28,145.36,32.21,.71,62.07-9.42,86.2-26.97l150.36,133.29c6.45,5.71,16.62,1.14,16.62-7.48V9.49C500,4.25,495.75,0,490.51,0H250Z"></path> </svg> Algolia </a> </div> </div> </form> </div> <div class="flex items-center h-100 ml-auto"> <a class="c-link c-link--icon-alone c-link--block m:hidden mx-1" id="search-link" aria-label="Search" href="/search"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-labelledby="a4y3klqtxkjsnakuin2cu6yygtpzv5ab" class="crayons-icon"><title id="a4y3klqtxkjsnakuin2cu6yygtpzv5ab">Search</title> <path d="M18.031 16.617l4.283 4.282-1.415 1.415-4.282-4.283A8.96 8.96 0 0111 20c-4.968 0-9-4.032-9-9s4.032-9 9-9 9 4.032 9 9a8.96 8.96 0 01-1.969 5.617zm-2.006-.742A6.977 6.977 0 0018 11c0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7a6.977 6.977 0 004.875-1.975l.15-.15z"></path> </svg> </a> <div class="flex" id="authentication-top-nav-actions"> <span class="hidden m:block"> <a href="/enter" class="c-link c-link--block mr-2 whitespace-nowrap ml-auto" data-no-instant> Log in </a> </span> <a href="/enter?state=new-user" data-tracking-id="ca_top_nav" data-tracking-source="top_navbar" class="c-cta c-cta--branded whitespace-nowrap mr-2" data-no-instant> Create account </a> </div> </div> </div> </header> <div class="hamburger"> <div class="hamburger__content"> <header class="hamburger__content__header"> <h2 class="fs-l fw-bold flex-1 break-word lh-tight">DEV Community</h2> <button class="c-btn c-btn--icon-alone js-hamburger-trigger shrink-0" aria-label="Close"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-labelledby="an74ggy3eu6gm7twnw441g1sj86dc2tn" aria-hidden="true" class="crayons-icon c-btn__icon"><title id="an74ggy3eu6gm7twnw441g1sj86dc2tn">Close</title><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636l4.95 4.95z"></path></svg> </button> </header> <div class="p-2 js-navigation-links-container" id="authentication-hamburger-actions"> </div> </div> <div class="hamburger__overlay js-hamburger-trigger"></div> </div> <div id="active-broadcast" class="broadcast-wrapper"></div> <div id="page-content" class="wrapper stories stories-show articletag-solidity articletag-gas articletag-blockchain articletag-calldata articleuser-353932" data-current-page="stories-show"> <div id="page-content-inner" data-internal-nav="false"> <div id="page-route-change" class="screen-reader-only" aria-live="polite" aria-atomic="true"></div> <style> .html-variant-wrapper { display: none} </style> <script src="https://unpkg.com/@webcomponents/webcomponentsjs@2.2.10/webcomponents-loader.js" integrity="sha384-3HK5hxQbkFqOIxMbpROlRmRtYl2LBZ52t+tqcjzsmr9NJuOWQxl8RgQSyFvq2lhy" crossorigin="anonymous" defer></script> <script src="https://assets.dev.to/assets/webShare-0686f0b9ac40589694ef6ae6a6202c44119bc781c254f6cf6d52d8a008461156.js" defer="defer"></script> <script src="https://assets.dev.to/assets/articlePage-e91bed0487621b06608474cfabffa186165e1f43f05b24581203ccc21e7aa45d.js" defer="defer"></script> <script src="https://assets.dev.to/assets/commentDropdowns-7a28d130e5b78d38b30a9495a964003a66bd64fa455fc70b766d69cf06b9ba24.js" defer="defer"></script> <script type="application/ld+json"> {"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c"},"url":"https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c","image":["https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otvb2z646ytpt1hl2rv.jpg","https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otvb2z646ytpt1hl2rv.jpg","https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otvb2z646ytpt1hl2rv.jpg"],"publisher":{"@context":"http://schema.org","@type":"Organization","name":"DEV Community","logo":{"@context":"http://schema.org","@type":"ImageObject","url":"https://media2.dev.to/dynamic/image/width=192,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png","width":"192","height":"192"}},"headline":"Gas Saving Techniques in Solidity","author":{"@context":"http://schema.org","@type":"Person","url":"https://dev.to/jamiescript","name":"Jamiebones"},"datePublished":"2022-07-09T20:12:55Z","dateModified":"2022-07-09T20:18:11Z"} </script> <div class="crayons-layout crayons-layout--3-cols crayons-layout--article"> <aside class="crayons-layout__sidebar-left" aria-label="Article actions"> <div class="crayons-article-actions print-hidden"> <div class="crayons-article-actions__inner"> <div class="reaction-drawer__outer hoverdown" style=""> <button id="reaction-drawer-trigger" aria-label="reaction-drawer-trigger" aria-pressed="false" class="hoverdown-trigger crayons-reaction pseudo-reaction crayons-tooltip__activator relative"> <span class="crayons-reaction__icon crayons-reaction__icon--borderless crayons-reaction--like crayons-reaction__icon--inactive" style="width: 40px; height: 40px"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" role="img" aria-hidden="true" class="crayons-icon"> <g clip-path="url(#clip0_988_3276)"> <path d="M19 14V17H22V19H18.999L19 22H17L16.999 19H14V17H17V14H19ZM20.243 4.75698C22.505 7.02498 22.583 10.637 20.479 12.992L19.059 11.574C20.39 10.05 20.32 7.65998 18.827 6.16998C17.324 4.67098 14.907 4.60698 13.337 6.01698L12.002 7.21498L10.666 6.01798C9.09103 4.60598 6.67503 4.66798 5.17203 6.17198C3.68203 7.66198 3.60703 10.047 4.98003 11.623L13.412 20.069L12 21.485L3.52003 12.993C1.41603 10.637 1.49503 7.01898 3.75603 4.75698C6.02103 2.49298 9.64403 2.41698 12 4.52898C14.349 2.41998 17.979 2.48998 20.242 4.75698H20.243Z" fill="#525252"></path> </g> <defs> <clipPath id="clip0_988_3276"> <rect width="24" height="24" fill="white"></rect> </clipPath> </defs> </svg> </span> <span class="crayons-reaction__icon crayons-reaction__icon--borderless crayons-reaction__icon--active" style="width: 40px; height: 40px"> <img aria_hidden="true" height="24" width="24" src="https://assets.dev.to/assets/heart-plus-active-9ea3b22f2bc311281db911d416166c5f430636e76b15cd5df6b3b841d830eefa.svg" /> </span> <span class="crayons-reaction__count" id="reaction_total_count"> <span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span> </span> <span class="crayons-tooltip__content"> Add reaction </span> </button> <div class="reaction-drawer" aria-expanded="false"> <div class="reaction-drawer__container"> <button id="reaction-butt-like" name="Like" aria-label="Like" aria-pressed="false" class="crayons-reaction crayons-tooltip__activator relative pt-2 pr-2 pb-1 pl-2" data-category="like"> <span class="crayons-reaction__icon crayons-reaction__icon--inactive p-0"> <img aria_hidden="true" height="32" width="32" src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" /> </span> <span class="crayons-reaction__count" id="reaction-number-like"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Like </span> </button> <button id="reaction-butt-unicorn" name="Unicorn" aria-label="Unicorn" aria-pressed="false" class="crayons-reaction crayons-tooltip__activator relative pt-2 pr-2 pb-1 pl-2" data-category="unicorn"> <span class="crayons-reaction__icon crayons-reaction__icon--inactive p-0"> <img aria_hidden="true" height="32" width="32" src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" /> </span> <span class="crayons-reaction__count" id="reaction-number-unicorn"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Unicorn </span> </button> <button id="reaction-butt-exploding_head" name="Exploding Head" aria-label="Exploding Head" aria-pressed="false" class="crayons-reaction crayons-tooltip__activator relative pt-2 pr-2 pb-1 pl-2" data-category="exploding_head"> <span class="crayons-reaction__icon crayons-reaction__icon--inactive p-0"> <img aria_hidden="true" height="32" width="32" src="https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg" /> </span> <span class="crayons-reaction__count" id="reaction-number-exploding_head"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Exploding Head </span> </button> <button id="reaction-butt-raised_hands" name="Raised Hands" aria-label="Raised Hands" aria-pressed="false" class="crayons-reaction crayons-tooltip__activator relative pt-2 pr-2 pb-1 pl-2" data-category="raised_hands"> <span class="crayons-reaction__icon crayons-reaction__icon--inactive p-0"> <img aria_hidden="true" height="32" width="32" src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" /> </span> <span class="crayons-reaction__count" id="reaction-number-raised_hands"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Raised Hands </span> </button> <button id="reaction-butt-fire" name="Fire" aria-label="Fire" aria-pressed="false" class="crayons-reaction crayons-tooltip__activator relative pt-2 pr-2 pb-1 pl-2" data-category="fire"> <span class="crayons-reaction__icon crayons-reaction__icon--inactive p-0"> <img aria_hidden="true" height="32" width="32" src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" /> </span> <span class="crayons-reaction__count" id="reaction-number-fire"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Fire </span> </button> </div> </div> </div> <button id="reaction-butt-comment" aria-label="Jump to Comments" aria-pressed="false" class="crayons-reaction crayons-reaction--comment crayons-tooltip__activator relative" data-category="comment"> <span class="crayons-reaction__icon crayons-reaction__icon--borderless crayons-reaction__icon--inactive"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" class="crayons-icon"> <path d="M10 3h4a8 8 0 010 16v3.5c-5-2-12-5-12-11.5a8 8 0 018-8zm2 14h2a6 6 0 000-12h-4a6 6 0 00-6 6c0 3.61 2.462 5.966 8 8.48V17z"></path> </svg> </span> <span class="crayons-reaction__count" id="reaction-number-comment" data-count="0"> <span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span> </span> <span data-testid="tooltip" class="crayons-tooltip__content"> Jump to Comments </span> </button> <button id="reaction-butt-readinglist" aria-label="Add to reading list" aria-pressed="false" class="crayons-reaction crayons-reaction--readinglist crayons-tooltip__activator relative" data-category="readinglist"> <span class="crayons-reaction__icon crayons-reaction__icon--borderless crayons-reaction__icon--inactive"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" class="crayons-icon"> <path d="M5 2h14a1 1 0 011 1v19.143a.5.5 0 01-.766.424L12 18.03l-7.234 4.536A.5.5 0 014 22.143V3a1 1 0 011-1zm13 2H6v15.432l6-3.761 6 3.761V4z"></path> </svg> </span> <span class="crayons-reaction__count" id="reaction-number-readinglist"><span class="bg-base-40 opacity-25 p-2 inline-block radius-default"></span></span> <span data-testid="tooltip" class="crayons-tooltip__content"> Save </span> </button> <button id="reaction-butt-boost" aria-label="Boost" aria-pressed="false" class="crayons-reaction crayons-reaction--boost crayons-tooltip__activator relative"> <span class="crayons-reaction__icon crayons-reaction__icon--borderless crayons-reaction__icon--inactive"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" role="img" aria-hidden="true" class="crayons-icon" width="24" height="24"> <path transform="translate(24,0) scale(-1,1)" d="M6 4H21C21.5523 4 22 4.44772 22 5V12H20V6H6V9L1 5L6 1V4ZM18 20H3C2.44772 20 2 19.5523 2 19V12H4V18H18V15L23 19L18 23V20Z"></path> </svg> </span> <span data-testid="tooltip" class="crayons-tooltip__content"> Boost </span> </button> <div id="mod-actions-menu-btn-area" class="print-hidden trusted-visible-block align-center"> </div> <div class="align-center m:relative"> <button id="article-show-more-button" aria-controls="article-show-more-dropdown" aria-expanded="false" aria-haspopup="true" class="dropbtn crayons-btn crayons-btn--ghost-dimmed crayons-btn--icon-rounded" aria-label="Share post options"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" role="img" aria-labelledby="ab7ze0f4civsfrpxyqwhivccdlqyaivn" aria-hidden="true" class="crayons-icon dropdown-icon"><title id="ab7ze0f4civsfrpxyqwhivccdlqyaivn">More...</title><path fill-rule="evenodd" clip-rule="evenodd" d="M7 12a2 2 0 11-4 0 2 2 0 014 0zm7 0a2 2 0 11-4 0 2 2 0 014 0zm5 2a2 2 0 100-4 2 2 0 000 4z"></path></svg> </button> <div id="article-show-more-dropdown" class="crayons-dropdown side-bar left-2 right-2 m:right-auto m:left-100 s:left-auto mb-1 m:mb-0 top-unset bottom-100 m:top-0 m:bottom-unset"> <div> <button id="copy-post-url-button" class="flex justify-between crayons-link crayons-link--block w-100 bg-transparent border-0" data-postUrl="https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c"> <span class="fw-bold">Copy link</span> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" id="article-copy-icon" role="img" aria-labelledby="aqxuyrkblk2dhsi9jpb9nudxbieo4a24" aria-hidden="true" class="crayons-icon mx-2 shrink-0"><title id="aqxuyrkblk2dhsi9jpb9nudxbieo4a24">Copy link</title> <path d="M7 6V3a1 1 0 011-1h12a1 1 0 011 1v14a1 1 0 01-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1 1 0 013 21l.003-14c0-.552.45-1 1.007-1H7zm2 0h8v10h2V4H9v2zm-2 5v2h6v-2H7zm0 4v2h6v-2H7z"></path> </svg> </button> <div id="article-copy-link-announcer" aria-live="polite" class="crayons-notice crayons-notice--success my-2 p-1" aria-live="polite" hidden>Copied to Clipboard</div> </div> <div class="Desktop-only"> <a target="_blank" class="crayons-link crayons-link--block" rel="noopener" href='https://twitter.com/intent/tweet?text=%22Gas%20Saving%20Techniques%20in%20Solidity%22%20by%20%40Jamiescript%20%23DEVCommunity%20https%3A%2F%2Fdev.to%2Fjamiescript%2Fgas-saving-techniques-in-solidity-324c'> Share to X </a> <a target="_blank" class="crayons-link crayons-link--block" rel="noopener" href="https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdev.to%2Fjamiescript%2Fgas-saving-techniques-in-solidity-324c&title=Gas%20Saving%20Techniques%20in%20Solidity&summary=Gas%20is%20a%20unit%20of%20computational%20measure%20in%20Solidity.%20This%20is%20the%20amount%20that%20we%20pay%20to%20interact%20and...&source=DEV%20Community"> Share to LinkedIn </a> <a target="_blank" class="crayons-link crayons-link--block" rel="noopener" href="https://www.facebook.com/sharer.php?u=https%3A%2F%2Fdev.to%2Fjamiescript%2Fgas-saving-techniques-in-solidity-324c"> Share to Facebook </a> <a target="_blank" class="crayons-link crayons-link--block" rel="noopener" href="https://toot.kytta.dev/?text=https%3A%2F%2Fdev.to%2Fjamiescript%2Fgas-saving-techniques-in-solidity-324c"> Share to Mastodon </a> </div> <web-share-wrapper shareurl="https://dev.to/jamiescript/gas-saving-techniques-in-solidity-324c" sharetitle="Gas Saving Techniques in Solidity" sharetext="Gas is a unit of computational measure in Solidity. This is the amount that we pay to interact and..." template="web-share-button"> </web-share-wrapper> <template id="web-share-button"> <a href="#" class="dropdown-link-row crayons-link crayons-link--block">Share Post via...</a> </template> <a href="/report-abuse" class="crayons-link crayons-link--block">Report Abuse</a> </div> </div> </div> </div> </aside> <main id="main-content" class="crayons-layout__content grid gap-4"> <div class="article-wrapper"> <article class="crayons-card crayons-article mb-4" id="article-show-container" data-article-id="1056635" data-article-slug="gas-saving-techniques-in-solidity-324c" data-author-id="353932" data-author-name="Jamiebones" data-author-username="jamiescript" data-co-author-ids="" data-path="/jamiescript/gas-saving-techniques-in-solidity-324c" data-pin-path="/stories/feed/pinned_article" data-pinned-article-id="" data-published="true" data-scheduled="false" > <header class="crayons-article__header" id="main-title"> <div class="crayons-article__header__meta"> <div class="flex s:items-start flex-col s:flex-row"> <div id="action-space" class="crayons-article__actions mb-4 s:mb-0 s:order-last"></div> <div class="flex flex-1 mb-5 items-start"> <div class="relative"> <a href="/jamiescript"><img class="radius-full align-middle" src="https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F353932%2F769950c3-6795-45bb-b2d3-159035ae9430.jpg" width="40" height="40" alt="Jamiebones" /></a> </div> <div class="pl-3 flex-1"> <a href="/jamiescript" class="crayons-link fw-bold">Jamiebones</a> <p class="fs-xs color-base-60"> Posted on <time datetime="2022-07-09T20:12:55Z" class="date">Jul 9, 2022</time> </p> </div> </div> </div> <div class="multiple_reactions_engagement"> <span class="reaction_engagement_like hidden"> <img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24" /> <span id="reaction_engagement_like_count">&nbsp;</span> </span> <span class="reaction_engagement_unicorn hidden"> <img src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" width="24" height="24" /> <span id="reaction_engagement_unicorn_count">&nbsp;</span> </span> <span class="reaction_engagement_exploding_head hidden"> <img src="https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg" width="24" height="24" /> <span id="reaction_engagement_exploding_head_count">&nbsp;</span> </span> <span class="reaction_engagement_raised_hands hidden"> <img src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" width="24" height="24" /> <span id="reaction_engagement_raised_hands_count">&nbsp;</span> </span> <span class="reaction_engagement_fire hidden"> <img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="24" height="24" /> <span id="reaction_engagement_fire_count">&nbsp;</span> </span> </div> <h1 class=" fs-3xl m:fs-4xl l:fs-5xl fw-bold s:fw-heavy lh-tight mb-2 medium"> Gas Saving Techniques in Solidity </h1> <div class="spec__tags flex flex-wrap"> <a class="crayons-tag " style=" --tag-bg: rgba(59, 73, 223, 0.10); --tag-prefix: #3b49df; --tag-bg-hover: rgba(59, 73, 223, 0.10); --tag-prefix-hover: #3b49df; " href="/t/solidity"><span class="crayons-tag__prefix">#</span>solidity</a> <a class="crayons-tag " style=" --tag-bg: rgba(59, 73, 223, 0.10); --tag-prefix: #3b49df; --tag-bg-hover: rgba(59, 73, 223, 0.10); --tag-prefix-hover: #3b49df; " href="/t/gas"><span class="crayons-tag__prefix">#</span>gas</a> <a class="crayons-tag " style=" --tag-bg: rgba(17, 58, 98, 0.10); --tag-prefix: #113a62; --tag-bg-hover: rgba(17, 58, 98, 0.10); --tag-prefix-hover: #113a62; " href="/t/blockchain"><span class="crayons-tag__prefix">#</span>blockchain</a> <a class="crayons-tag " style=" --tag-bg: rgba(59, 73, 223, 0.10); --tag-prefix: #3b49df; --tag-bg-hover: rgba(59, 73, 223, 0.10); --tag-prefix-hover: #3b49df; " href="/t/calldata"><span class="crayons-tag__prefix">#</span>calldata</a> </div> </div> </header> <div class="crayons-article__main "> <div class="crayons-article__body text-styles spec__body" data-article-id="1056635" id="article-body"> <p><code>Gas</code> is a unit of computational measure in Solidity. This is the amount that we pay to interact and transact with a smart contracts. The <code>gas</code> is calculated via a simple formulae which is:<br> <code>Gas = Gas Price * Gas used</code>.<br> The <code>gas</code> price is not constant and it is dependent on the congestion of the network. The more users pushing transaction through the network the higher the <code>gas</code> price. The amount of <code>gas</code> used is determined by the operation performed by the smart contract. </p> <p>This blog post will discuss some techniques we might employ to reduce the <code>gas</code> spent when sending transaction to a smart contract. We will consider the following listed techniques to reduce the <code>gas</code> foot print of a smart contract.</p> <ul> <li> <a href="#constant">Use constant and immutable variables for variable that don't change</a> </li> <li> <a href="#cache">Cache read variables in memory</a> </li> <li> <a href="#incrementing">Incrementing and decrementing by 1</a> </li> <li> <a href="#calldata">calldata and memory </a> </li> <li> <a href="#view">Calling view function </a> </li> <li> <a href="#overflow">Integer overflow/underflow </a> </li> <li> <a href="#revert">Use revert instead of require </a> </li> <li> <a href="#memory">Avoid loading too many data in memory</a> </li> <li> <a href="#tips">Other tips</a> </li> </ul> <h2> <a name="use-constant-and-immutable-variables-for-variable-that-dont-change" href="#use-constant-and-immutable-variables-for-variable-that-dont-change"> </a> <a name="constant"></a> Use constant and immutable variables for variable that don't change </h2> <p>Using the <code>constant</code> and the <code>immutable</code> keywords for variables that do not change helps to save on <code>gas</code> used. The reason been that <code>constant</code> and <code>immutable</code> variables do not occupy a storage slot when compiled. They are saved inside the contract byte code. Lets see an example.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="kr">interface</span> <span class="nx">IERC20</span> <span class="p">{</span> <span class="kd">function</span> <span class="nx">transfer</span><span class="p">(</span><span class="nx">address</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">uint256</span> <span class="nx">value</span><span class="p">)</span> <span class="nx">external</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">bool</span><span class="p">);</span> <span class="kd">function</span> <span class="nx">approve</span><span class="p">(</span><span class="nx">address</span> <span class="nx">spender</span><span class="p">,</span> <span class="nx">uint256</span> <span class="nx">value</span><span class="p">)</span> <span class="nx">external</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">bool</span><span class="p">);</span> <span class="kd">function</span> <span class="nx">transferFrom</span><span class="p">(</span><span class="nx">address</span> <span class="k">from</span><span class="p">,</span> <span class="nx">address</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">uint256</span> <span class="nx">value</span><span class="p">)</span> <span class="nx">external</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">bool</span><span class="p">);</span> <span class="c1">//rest of the interface code</span> <span class="p">}</span> <span class="c1">//Gas used: 187302</span> <span class="nx">contract</span> <span class="nx">Expensive</span> <span class="p">{</span> <span class="nx">IERC20</span> <span class="kr">public</span> <span class="nx">token</span><span class="p">;</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">timeStamp</span> <span class="o">=</span> <span class="mi">566777</span><span class="p">;</span> <span class="kd">constructor</span><span class="p">(</span><span class="nx">address</span> <span class="nx">token_address</span><span class="p">)</span> <span class="p">{</span> <span class="nx">token</span> <span class="o">=</span> <span class="nx">IERC20</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> <span class="c1">//Gas used: 142890</span> <span class="nx">contract</span> <span class="nx">GasSaver</span> <span class="p">{</span> <span class="nx">IERC20</span> <span class="kr">public</span> <span class="nx">immutable</span> <span class="nx">i_token</span><span class="p">;</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">constant</span> <span class="nx">TIMESTAMP</span> <span class="o">=</span> <span class="mi">566777</span><span class="p">;</span> <span class="kd">constructor</span><span class="p">(</span><span class="nx">address</span> <span class="nx">token_address</span><span class="p">)</span> <span class="p">{</span> <span class="nx">i_token</span> <span class="o">=</span> <span class="nx">IERC20</span><span class="p">(</span><span class="nx">token_address</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The first contract <code>Expensive</code> has a public variable of type <code>IERC20</code> and a <code>uint256</code> also defined as public. When we deployed this contract, the contract deployment used up a total of 187302 <code>gas</code> when deployed to Remix. The second contract named <code>GasSaver</code> also have the same variable structure as the <code>Expensive</code> contract but we made use of <code>constant</code> keyword for the <code>TIMESTAMP</code> because we will not be changing the value. For the <code>interface</code> we used the <code>immutable</code> keyword which means we can no longer modify the value of the <code>i_token</code> variable after setting it inside the <code>constructor</code>. This action alone reduced the gas consumption from 187302 to 142890.</p> <h2> <a name="cache-read-variables-in-memory" href="#cache-read-variables-in-memory"> </a> <a name="constant"></a> Cache read variables in memory </h2> <p>Reading from a variable in storage cost <code>gas</code>. When we access a variable for the first time it cost <code>2100 gas</code> and subsequent access will cost <code>100 gas</code>. If we have an array and want to perform some computation on the array elements. It could be more gas effective to read the <code>length</code> of the array and store it in a variable. If the array is not too large we could also read the whole <code>array</code> into memory and access it from memory instead of continuously reading from storage.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">Expensive</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[]</span> <span class="kr">public</span> <span class="nx">numbers</span><span class="p">;</span> <span class="kd">constructor</span><span class="p">(</span><span class="nx">uint256</span><span class="p">[]</span> <span class="nx">memory</span> <span class="nx">_numbers</span><span class="p">)</span> <span class="p">{</span> <span class="nx">numbers</span> <span class="o">=</span> <span class="nx">_numbers</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//Gas used: 40146</span> <span class="kd">function</span> <span class="nx">sum</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">view</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span> <span class="p">){</span> <span class="nx">uint256</span> <span class="nx">total</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="nx">uint256</span> <span class="nx">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">numbers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="nx">total</span> <span class="o">+=</span> <span class="nx">numbers</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> <span class="p">}</span> <span class="k">return</span> <span class="nx">total</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">GasSaver</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[]</span> <span class="kr">public</span> <span class="nx">numbers</span><span class="p">;</span> <span class="kd">constructor</span><span class="p">(</span><span class="nx">uint256</span><span class="p">[]</span> <span class="nx">memory</span> <span class="nx">_numbers</span><span class="p">)</span> <span class="p">{</span> <span class="nx">numbers</span> <span class="o">=</span> <span class="nx">_numbers</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//39434</span> <span class="kd">function</span> <span class="nx">sum</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">view</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span> <span class="p">){</span> <span class="nx">uint256</span> <span class="nx">total</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">uint256</span> <span class="nx">arrLength</span> <span class="o">=</span> <span class="nx">numbers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">uint256</span><span class="p">[]</span> <span class="nx">memory</span> <span class="nx">_numbersInMemory</span> <span class="o">=</span> <span class="nx">numbers</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="nx">uint256</span> <span class="nx">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">arrLength</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="nx">total</span> <span class="o">+=</span> <span class="nx">_numbersInMemory</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> <span class="p">}</span> <span class="k">return</span> <span class="nx">total</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The contract <code>GasSaver</code> above saved the length of the array in a variable <code>arrLength</code> and also loaded the array into memory. This helps to reduce the <code>gas</code> spent inside the function.</p> <h2> <a name="incrementing-and-decrementing-by-1" href="#incrementing-and-decrementing-by-1"> </a> <a name="incrementing"></a> Incrementing and Decrementing by 1 </h2> <p>There are four ways to increment and decrement a variable by 1 in Solidity. Let us see an example.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">One</span><span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">number</span><span class="p">;</span> <span class="c1">//Gas used : 43800</span> <span class="kd">function</span> <span class="nx">incrementByOne</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span><span class="p">){</span> <span class="nx">number</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">return</span> <span class="nx">number</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">Two</span><span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">number</span><span class="p">;</span> <span class="c1">//Gas used : 43787</span> <span class="kd">function</span> <span class="nx">incrementByOne</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span><span class="p">){</span> <span class="nx">number</span> <span class="o">=</span> <span class="nx">number</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">return</span> <span class="nx">number</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">Three</span><span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">number</span><span class="p">;</span> <span class="c1">//Gas used : 43634</span> <span class="kd">function</span> <span class="nx">incrementByOne</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span><span class="p">){</span> <span class="k">return</span> <span class="nx">number</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">Four</span><span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">number</span><span class="p">;</span> <span class="c1">//Gas used : 43628</span> <span class="kd">function</span> <span class="nx">incrementByOne</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span><span class="p">){</span> <span class="k">return</span> <span class="o">++</span><span class="nx">number</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The example above list four contracts which shows the way we could increment a number variable by a value of 1. The contract <code>Four</code> is more gas effective out of the lot when ran. To save more on <code>gas</code> when incrementing a variable use the method of increment in contract <code>Four</code> (<code>++number</code>) which is more <code>gas</code> efficient.</p> <h2> <a name="calldata-and-memory" href="#calldata-and-memory"> </a> <a name="calldata"></a> calldata and memory </h2> <p>When running a function we could pass the function parameters as <code>calldata</code> or <code>memory</code> for variables such as <code>strings</code>, <code>structs</code>,<code>arrays</code> etc. If we are not modifying the passed parameter we should pass it as <code>calldata</code> because <code>calldata</code> is more <code>gas</code> efficient than <code>memory</code>. Let's see an example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">GasSaver</span> <span class="p">{</span> <span class="c1">//Gas used: 22471</span> <span class="kd">function</span> <span class="nx">passParameterAsCallData</span><span class="p">(</span><span class="nx">string</span> <span class="nx">calldata</span> <span class="nx">_name</span><span class="p">)</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">string</span> <span class="nx">memory</span><span class="p">){}</span> <span class="c1">//Gas used: 22913</span> <span class="kd">function</span> <span class="nx">passParameterAsMemory</span><span class="p">(</span><span class="nx">string</span> <span class="nx">memory</span> <span class="nx">_name</span><span class="p">)</span> <span class="kr">public</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">string</span> <span class="nx">memory</span><span class="p">){}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>Calling these two functions in Remix and passing the same parameter to the functions, you will noticed that setting a function <code>_name</code> parameter as <code>calldata</code> reduced the <code>gas</code> used. </p> <blockquote> <p>You can only used <code>calldata</code> when you are not going to modify the value of the variable passed as <code>calldata</code> inside the function.</p> </blockquote> <h2> <a name="calling-a-raw-view-endraw-function" href="#calling-a-raw-view-endraw-function"> </a> <a name="view"></a> Calling a <code>view</code> function </h2> <p>A <code>view</code> function does not use <code>gas</code> when called, but if we decide to call a view function inside of another function which is a transaction, it then uses <code>gas</code>.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">GasSaver</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[]</span> <span class="kr">private</span> <span class="nx">numbers</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">67</span><span class="p">,</span><span class="mi">34</span><span class="p">];</span> <span class="kd">function</span> <span class="nx">getNumberAt</span><span class="p">(</span> <span class="nx">uint256</span> <span class="nx">_index</span> <span class="p">)</span> <span class="kr">public</span> <span class="nx">view</span> <span class="nx">returns</span> <span class="p">(</span><span class="nx">uint256</span><span class="p">){</span> <span class="k">return</span> <span class="nx">numbers</span><span class="p">[</span><span class="nx">_index</span><span class="p">];</span> <span class="p">}</span> <span class="c1">//Gas used when getNumber was called: 44778</span> <span class="c1">//Gas used without calling getNumber() 44450</span> <span class="kd">function</span> <span class="nx">sumAndMultiply</span><span class="p">()</span> <span class="kr">public</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[]</span> <span class="nx">memory</span> <span class="nx">_numbers</span> <span class="o">=</span> <span class="nx">numbers</span><span class="p">;</span> <span class="nx">uint256</span> <span class="nx">arrlength</span> <span class="o">=</span> <span class="nx">_numbers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="k">for</span><span class="p">(</span><span class="nx">uint256</span> <span class="nx">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">arrlength</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">){</span> <span class="nx">numbers</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_numbers</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">*</span> <span class="nx">i</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//getNumberAt(2);</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The above is a simple contract to illustrate the point about calling <code>view</code> functions inside of a transaction. The function <code>getNumberAt</code> is a <code>view</code> function that returns the value of the array at the passed index. Calling this function uses no <code>gas</code> because it is a view function, but if we decide to call this function inside of the <code>sumAndMultiply</code> function, we can see that the <code>gas</code> usage of <code>sumAndMultiply</code> <br> has increased because the <code>view</code> function is called within it.</p> <p><code>Gas</code> used when <code>sumAndMutiply</code> functions is called: <code>44778</code>.<br> <code>Gas</code> used when <code>getNumberAt</code> <code>view</code> function is also called inside the <code>sumAndMultiply</code> function: <code>44450</code>.</p> <p>So, we can see that calling a <code>view</code> function inside a transaction increased the amount of <code>gas</code> that should have been spent, if the <code>view</code> function was not included in the call.</p> <h2> <a name="integer-overflowunderflow" href="#integer-overflowunderflow"> </a> <a name="overflow"></a> Integer overflow/underflow </h2> <p>Prior to Solidity <code>0.8.0</code> version, interger overflow and underflow checks were performed by using the <code>SafeMath</code> library. From Solidity <code>0.8.0</code> upward, the compiler does that check for us. </p> <p>This extra check cost <code>gas</code>. If we know that the mathematical operations we will perform inside the contract will not overflow or underflow, we can tell the compiler not to check for overflow or underflow in the operation.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">OverFlow</span> <span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">numberOne</span><span class="p">;</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">numberTwo</span><span class="p">;</span> <span class="c1">//Gas used: 43440</span> <span class="kd">function</span> <span class="nx">setNumberOne</span><span class="p">()</span> <span class="kr">public</span> <span class="p">{</span> <span class="o">++</span><span class="nx">numberOne</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//Gas used: 43339</span> <span class="kd">function</span> <span class="nx">setNumberTwo</span><span class="p">()</span> <span class="kr">public</span> <span class="p">{</span> <span class="nx">unchecked</span> <span class="p">{</span> <span class="o">++</span><span class="nx">numberTwo</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The above contract <code>OverFlow</code> has two functions used to set two variables inside the contract. The compiler in <code>setNumberOne</code> checks and prevents the number from overflow or underflow. In the second function <code>setNumberTwo</code> we are telling the compiler to mind its business and not check for overflow or underflow. Using <code>unchecked</code> means that the code block will not be checked for overflow or underflow.</p> <p>If we know that our mathematics will be safe we could safe a little gas by using <code>unchecked</code>.</p> <h2> <a name="use-revert-instead-of-require" href="#use-revert-instead-of-require"> </a> <a name="revert"></a> Use revert instead of require </h2> <p>We use <code>require</code> to check if a statement is <code>true</code>. If the statement evaluates to false the transaction is reverted and the remaining gas returned to the user. Since the advert of custom error in Solidity we could use the <code>revert</code> statement to throw a custom error. Using <code>revert</code> instead of <code>require</code> is more gas efficient. You can run the code below to see the the amount of <code>gas</code> saved when <code>revert</code> is used instead of <code>require</code>.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">error</span> <span class="nx">NotEnough</span><span class="p">();</span> <span class="nx">contract</span> <span class="nx">GasSaver</span> <span class="p">{</span> <span class="nx">uint256</span> <span class="kr">public</span> <span class="nx">number</span><span class="p">;</span> <span class="c1">//Gas Used: 21898</span> <span class="kd">function</span> <span class="nx">setNumber</span><span class="p">(</span><span class="nx">uint256</span> <span class="nx">_number</span><span class="p">)</span> <span class="kr">public</span> <span class="p">{</span> <span class="nx">require</span><span class="p">(</span><span class="nx">_number</span> <span class="o">&gt;</span> <span class="mi">10</span><span class="p">,</span> <span class="dl">"</span><span class="s2">number too small please</span><span class="dl">"</span><span class="p">);</span> <span class="nx">number</span> <span class="o">=</span> <span class="nx">_number</span><span class="p">;</span> <span class="p">}</span> <span class="c1">//Gas Used: 21669</span> <span class="kd">function</span> <span class="nx">setNumberAndRevert</span><span class="p">(</span><span class="nx">uint256</span> <span class="nx">_number</span><span class="p">)</span> <span class="kr">public</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span> <span class="nx">_number</span> <span class="o">&lt;</span> <span class="mi">10</span> <span class="p">){</span> <span class="nx">revert</span> <span class="nx">NotEnough</span><span class="p">();</span> <span class="p">}</span> <span class="nx">number</span> <span class="o">=</span> <span class="nx">_number</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <h2> <a name="avoid-loading-too-many-data-in-memory" href="#avoid-loading-too-many-data-in-memory"> </a> <a name="memory"></a> Avoid loading too many data in memory </h2> <p>Loading data in memory in a single transaction has the effect of increasing <code>gas</code>used. The <code>gas</code> used increase in a quadratic factor when more than <code>32kb</code> of memory is used in a single transaction. Let's see an example.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="c1">//SPDX-License-Identifier:MIT;</span> <span class="nx">pragma</span> <span class="nx">solidity</span> <span class="o">^</span><span class="mf">0.8</span><span class="p">.</span><span class="mi">3</span><span class="p">;</span> <span class="nx">contract</span> <span class="nx">One</span> <span class="p">{</span> <span class="c1">//Gas used: 29261</span> <span class="kd">function</span> <span class="nx">setArrayInMemory</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">pure</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[</span><span class="mi">1000</span><span class="p">]</span> <span class="nx">memory</span> <span class="nx">_array</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">Two</span> <span class="p">{</span> <span class="c1">//Gas used: 276761</span> <span class="kd">function</span> <span class="nx">setArrayInMemory</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">pure</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[</span><span class="mi">10000</span><span class="p">]</span> <span class="nx">memory</span> <span class="nx">_array</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="c1">//Gas used: 922855</span> <span class="nx">contract</span> <span class="nx">Three</span> <span class="p">{</span> <span class="kd">function</span> <span class="nx">setArrayInMemory</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">pure</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[</span><span class="mi">20000</span><span class="p">]</span> <span class="nx">memory</span> <span class="nx">_array</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">contract</span> <span class="nx">Four</span> <span class="p">{</span> <span class="c1">//Gas used: 3386918</span> <span class="kd">function</span> <span class="nx">setArrayInMemory</span><span class="p">()</span> <span class="kr">public</span> <span class="nx">pure</span> <span class="p">{</span> <span class="nx">uint256</span><span class="p">[</span><span class="mi">40000</span><span class="p">]</span> <span class="nx">memory</span> <span class="nx">_array</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The above example defined four contracts and each contract has a function named <code>setArrayInMemory</code> where memory space is reserved for an array of <code>uint256</code>. </p> <p>Contract <code>One</code> loads reserves space in memory for 1000 <code>uint256</code> numbers and when the function is executed it uses up 29261 <code>gas</code>. Contract <code>Two</code> creates memory space for 10000 <code>uint256</code> numbers and we can see that the <code>gas</code> used is 276761 which is roughly about 10 times of the <code>gas</code> used by Contract <code>One</code>.</p> <p>Things gets interesting in Contract <code>Three</code> which reserves space for 20000 <code>uint256</code> numbers. From the <code>gas</code> used which is 922855, we can see that once the memory used in a single transaction exceeds 32 kilobyte, the quadratic factor of the memory expansion cost kicks in thereby increasing the <code>gas</code> used in a quadratic way.</p> <blockquote> <p>Do not populate arrays with enormous amounts of items in a single transaction to prevent <code>gas</code> used increase in a quadratic way.</p> </blockquote> <h2> <a name="other-tips" href="#other-tips"> </a> <a name="tips"></a> Other tips </h2> <ul> <li><p>Always put your <code>require</code> statement on top in the function before making any state change so that if the <code>require</code> statement fails, the remaining <code>gas</code> is refunded to the user on revert.</p></li> <li><p>When comparing operation using <code>&amp;&amp;</code> or <code>||</code> with a <code>require</code> statement; you should put the cheaper part of the operation first so that if it fails, the compiler will not compare the second part thereby saving on <code>gas</code> used.</p></li> </ul> <p>In conclusion We can use a combination of these techniques highlighted here to improve the <code>gas</code> consumption of our smart contracts thereby saving our users from spending too much on <code>gas</code> when interacting with our smart contracts.</p> <p>Thanks for reading.</p> </div> <div class="js-billboard-container body-billboard-container" data-async-url="/jamiescript/gas-saving-techniques-in-solidity-324c/billboards/post_body_bottom"></div> </div> <section id="comments" data-follow-button-container="true" data-updated-at="2025-01-05 22:25:31 UTC" class="text-padding mb-4 border-t-1 border-0 border-solid border-base-10"> <header class="relative flex justify-between items-center mb-6"> <div class="flex items-center"> <h2 class="crayons-subtitle-1"> Top comments <span class="js-comments-count" data-comments-count="0">(0)</span> </h2> </div> <div id="comment-subscription" class="print-hidden"> <div class="crayons-btn-group"> <span class="crayons-btn crayons-btn--outlined">Subscribe</span> </div> </div> </header> <div id="billboard_delay_trigger"></div> <div id="comments-container" data-testid="comments-container" data-commentable-id="1056635" data-commentable-type="Article" data-has-recent-comment-activity="false"> <div id="response-templates-data" class="hidden"></div> <form class="comment-form print-hidden" id="new_comment" action="/comments" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" /> <input type="hidden" name="authenticity_token" value="NOTHING" id="new_comment_authenticity_token"> <input value="1056635" autocomplete="off" type="hidden" name="comment[commentable_id]" id="comment_commentable_id" /> <input value="Article" autocomplete="off" type="hidden" name="comment[commentable_type]" id="comment_commentable_type" /> <span class="crayons-avatar m:crayons-avatar--l mr-2 shrink-0"> <img src="https://media2.dev.to/dynamic/image/width=256,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" width="32" height="32" alt="pic" class="crayons-avatar__image overflow-hidden" id="comment-primary-user-profile--avatar" loading="lazy" /> </span> <div class="comment-form__inner"> <div class="comment-form__field" data-tracking-name="comment_form_textfield"> <textarea placeholder="Add to the discussion" onfocus="handleFocus(event)" onkeyup="handleKeyUp(event)" onkeydown="handleKeyDown(event)" oninput="handleChange(event)" id="text-area" required="required" class="crayons-textfield comment-textarea crayons-textfield--ghost" aria-label="Add a comment to the discussion" name="comment[body_markdown]"> </textarea> </div> <div class="response-templates-container crayons-card crayons-card--secondary p-4 mb-4 comment-form__templates fs-base hidden"> <header class="mb-3"> <button type="button" class="crayons-btn personal-template-button active" data-target-type="personal" data-form-id="new_comment">Personal</button> <button type="button" class="crayons-btn moderator-template-button hidden" data-target-type="moderator" data-form-id="new_comment">Trusted User</button> </header> <div class="personal-responses-container"> </div> <div class="moderator-responses-container hidden"> </div> <a target="_blank" rel="noopener nofollow" href="/settings/response-templates"> Create template </a> <p>Templates let you quickly answer FAQs or store snippets for re-use.</p> </div> <div class="comment-form__preview text-styles text-styles--secondary" id="preview-div"></div> <div class="comment-form__buttons mb-4"> <button type="submit" class="crayons-btn mr-2 js-btn-enable" onclick="validateField(event)" data-tracking-name="comment_submit_button" disabled>Submit</button> <button type="button" class="preview-toggle crayons-btn crayons-btn--secondary comment-action-preview js-btn-enable mr-2" data-tracking-name="comment_preview_button" disabled>Preview</button> <a href="/404.html" class="dismiss-edit-comment crayons-btn crayons-btn--ghost js-btn-dismiss hidden">Dismiss</a> </div> </div> <div class="code-of-conduct" id="toggle-code-of-conduct-checkbox"></div> </form> <div class="comments" id="comment-trees-container"> </div> </div> <div class="align-center"> <nav class="fs-s align-center block" aria-label="Conduct controls"> <a href="/code-of-conduct" class="crayons-link crayons-link--secondary">Code of Conduct</a> <span class="opacity-25 px-2" role="presentation">&bull;</span> <a href="/report-abuse" class="crayons-link crayons-link--secondary">Report abuse</a> </nav> </div> </section> <div id="hide-comments-modal" class="hidden"> <form id="hide-comments-modal__form" class="hide-comments-modal__form" data-type="json" action="/comments/hide" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" /><input type="hidden" name="_method" value="patch" autocomplete="off" /><input type="hidden" name="authenticity_token" value="tYr9f15p2iIa682xMLhW2rwq5r3lgG2nP-vLF41Qk9_0sXb07wQPUUJ-ApdwEAsk6fBh46u1uwNKjSDq8gxafQ" autocomplete="off" /> <div class="hide-comments-modal__content"> <p class="pb-2"> Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's <a id="hide-comments-modal__comment-permalink" href="#">permalink</a>. </p> <label class="crayons-field crayons-field--checkbox my-2"> <input name="hide_children" type="hidden" value="0" autocomplete="off" /><input class="hide_children crayons-checkbox" type="checkbox" value="1" name="hide_children" id="hide_children" /> <p class="crayons-field__label">Hide child comments as well</p> </label> <p class="pb-4 pt-2"> <button type="submit" class="crayons-btn"> Confirm </button> </p> </div> </form> <p class="fs-s color-base-60">For further actions, you may consider blocking this person and/or <a id="hide-comments-modal__report-link" href="/report-abuse">reporting abuse</a></p> </div> </article> <div class="pb-4 crayons-layout__comments-billboard"> <div class="js-billboard-container pb-4 crayons-layout__comments-billboard" data-async-url="/jamiescript/gas-saving-techniques-in-solidity-324c/billboards/post_comments"></div> </div> <section class="crayons-card crayons-card--secondary text-padding mb-4 print-hidden" id="bottom-content-read-next"> <h2 class="crayons-subtitle-1">Read next</h2> <a href="/louwers/bundling-without-a-bundler-with-esmsh-497d" data-preload-image="" class="crayons-link mt-6 block"> <div class="flex items-center"> <span class="crayons-avatar crayons-avatar--xl m:crayons-avatar--2xl mr-4 shrink-0"> <img loading="lazy" alt="louwers profile image" class="crayons-avatar__image" width="100" height="100" src="https://media2.dev.to/dynamic/image/width=100,height=100,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F9011%2Fe13d89cf-2e12-4099-afd7-88ee5fa63396.jpeg" /> </span> <div> <h3 class="fs-xl mb-0 lh-tight">Bundling without a bundler with esm.sh</h3> <p class="opacity-75 pt-1"> Bart Louwers - <time datetime="2025-01-05T13:31:51Z">Jan 5</time> </p> </div> </div> </a> <a href="/faizantareen383/turning-mistakes-into-growth-as-a-developer-4ibf" data-preload-image="" class="crayons-link mt-6 block"> <div class="flex items-center"> <span class="crayons-avatar crayons-avatar--xl m:crayons-avatar--2xl mr-4 shrink-0"> <img loading="lazy" alt="faizantareen383 profile image" class="crayons-avatar__image" width="100" height="100" src="https://media2.dev.to/dynamic/image/width=100,height=100,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1503850%2Fbcfa02d5-3195-4f0e-b19d-285c3f943edd.png" /> </span> <div> <h3 class="fs-xl mb-0 lh-tight">Turning Mistakes Into Growth as a Developer</h3> <p class="opacity-75 pt-1"> Faizan Tareen - <time datetime="2025-01-05T13:10:47Z">Jan 5</time> </p> </div> </div> </a> <a href="/daky2024/blockchain-the-emerging-trend-2f1e" data-preload-image="https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ivh0ebt2ybqhrqwckf1.jpeg" class="crayons-link mt-6 block"> <div class="flex items-center"> <span class="crayons-avatar crayons-avatar--xl m:crayons-avatar--2xl mr-4 shrink-0"> <img loading="lazy" alt="daky2024 profile image" class="crayons-avatar__image" width="100" height="100" src="https://media2.dev.to/dynamic/image/width=100,height=100,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2153584%2F299dbbaa-73b1-413c-8902-c6fdf4af2717.jpeg" /> </span> <div> <h3 class="fs-xl mb-0 lh-tight">BLOCKCHAIN the emerging trend</h3> <p class="opacity-75 pt-1"> Dakshata - <time datetime="2025-01-05T18:09:15Z">Jan 5</time> </p> </div> </div> </a> <a href="/ramunarasinga-11/renovatejson-file-in-lobechat-source-code-explained-4320" data-preload-image="" class="crayons-link mt-6 block"> <div class="flex items-center"> <span class="crayons-avatar crayons-avatar--xl m:crayons-avatar--2xl mr-4 shrink-0"> <img loading="lazy" alt="ramunarasinga-11 profile image" class="crayons-avatar__image" width="100" height="100" src="https://media2.dev.to/dynamic/image/width=100,height=100,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F392818%2Fcc9dbd66-5cc2-4591-8c1c-6c650a374161.jpeg" /> </span> <div> <h3 class="fs-xl mb-0 lh-tight">renovate.json file in Lobechat source code explained</h3> <p class="opacity-75 pt-1"> Ramu Narasinga - <time datetime="2025-01-05T13:11:58Z">Jan 5</time> </p> </div> </div> </a> </section> </div> </main> <aside class="crayons-layout__sidebar-right" aria-label="Author details"> <div class="crayons-article-sticky grid gap-4 pb-4 break-word" id="article-show-primary-sticky-nav"> <div class="crayons-card crayons-card--secondary branded-7 p-4 pt-0 gap-4 grid" style="border-top-color: #000000;"> <div class="-mt-4"> <a href="/jamiescript" class="flex"> <span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"> <img src="https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F353932%2F769950c3-6795-45bb-b2d3-159035ae9430.jpg" class="crayons-avatar__image" alt="" loading="lazy" /> </span> <span class="crayons-link crayons-subtitle-2 mt-5"> Jamiebones </span> </a> </div> <div class="print-hidden"> <button name="button" type="button" data-info="{&quot;className&quot;:&quot;User&quot;,&quot;style&quot;:&quot;&quot;,&quot;id&quot;:353932,&quot;name&quot;:&quot;Jamiebones&quot;}" class="crayons-btn follow-action-button whitespace-nowrap w-100 follow-user" aria-label="Follow user: Jamiebones" aria-pressed="false">Follow</button> </div> <div class="color-base-70"> Javascript developer working hard to master the craft. </div> <div class="user-metadata-details"> <ul class="user-metadata-details-inner"> <li> <div class="key"> Location </div> <div class="value"> Uyo, Nigeria </div> </li> <li> <div class="key"> Education </div> <div class="value"> University of Ilorin </div> </li> <li> <div class="key"> Work </div> <div class="value"> Author @ Educative.io </div> </li> <li> <div class="key"> Joined </div> <div class="value"> <time datetime="2020-03-22T13:16:17Z" class="date">Mar 22, 2020</time> </div> </li> </ul> </div> </div> <div class="crayons-card crayons-card--secondary"> <header class="crayons-card__header"> <h3 class="crayons-subtitle-2"> More from <a href="/jamiescript">Jamiebones</a> </h3> </header> <div> <a class="crayons-link crayons-link--contentful" href="/jamiescript/how-to-create-and-deploy-an-nft-smart-contract-2gj8"> How to Create and Deploy an NFT Smart Contract <div class="crayons-link__secondary -ml-1"> <span class="mr-1"><span class="opacity-50">#</span>solidity</span> <span class="mr-1"><span class="opacity-50">#</span>nft</span> <span class="mr-1"><span class="opacity-50">#</span>smartcontract</span> </div> </a> <a class="crayons-link crayons-link--contentful" href="/jamiescript/how-to-call-other-contracts-in-solidity-16dh"> How To Call Other Contracts In Solidity <div class="crayons-link__secondary -ml-1"> <span class="mr-1"><span class="opacity-50">#</span>smartcontract</span> <span class="mr-1"><span class="opacity-50">#</span>solidity</span> <span class="mr-1"><span class="opacity-50">#</span>blockchain</span> <span class="mr-1"><span class="opacity-50">#</span>ethereum</span> </div> </a> <a class="crayons-link crayons-link--contentful" href="/jamiescript/creating-upgradable-solidity-contract-with-hardhat-1134"> Creating Upgradable Solidity Contract With Hardhat <div class="crayons-link__secondary -ml-1"> <span class="mr-1"><span class="opacity-50">#</span>openzepplin</span> <span class="mr-1"><span class="opacity-50">#</span>upgradables</span> <span class="mr-1"><span class="opacity-50">#</span>solidity</span> <span class="mr-1"><span class="opacity-50">#</span>hardhat</span> </div> </a> </div> </div> </div> <div class="crayons-article-sticky grid gap-4 break-word js-billboard-container" data-async-url="/jamiescript/gas-saving-techniques-in-solidity-324c/billboards/post_sidebar"></div> </aside> </div> <div class="mod-actions-menu print-hidden"></div> <div data-testid="unpublish-post-modal-container" class="unpublish-post-modal-container hidden"></div> <div class="fullscreen-code js-fullscreen-code"></div> <script src="https://assets.dev.to/assets/followButtons-f2455d1f50a862b83fa006b1953e3a1644598781243cae25d3e75b13c04184fd.js" defer="defer"></script> <script src="https://assets.dev.to/assets/billboard-a7ffd2b38d410444a4ef2d42c6ce905699d84b1520aed314b817a767ddc3b362.js" defer="defer"></script> <script src="https://assets.dev.to/assets/localizeArticleDates-70147c5c6bfe350b42e020ebb2a3dd37419d83978982b5a67b6389119bf162ac.js" defer="defer"></script> <script src="https://assets.dev.to/assets/articleReactions-79a44d82fb34a91c3987c24cfd3bcf2b0d38a02730b69ae6da297387d185f015.js" defer="defer"></script> <script> function activateRunkitTags() { if (!areAnyRunkitTagsPresent()) return var checkRunkit = setInterval(function() { try { dynamicallyLoadRunkitLibrary() if (typeof(RunKit) === 'undefined') { return } replaceTagContentsWithRunkitWidget() clearInterval(checkRunkit); } catch(e) { console.error(e); clearInterval(checkRunkit); } }, 200); } function isRunkitTagAlreadyActive(runkitTag) { return runkitTag.querySelector("iframe") !== null; }; function areAnyRunkitTagsPresent() { var presentRunkitTags = document.getElementsByClassName("runkit-element"); return presentRunkitTags.length > 0 } function replaceTagContentsWithRunkitWidget() { var targets = document.getElementsByClassName("runkit-element"); for (var i = 0; i < targets.length; i++) { if (isRunkitTagAlreadyActive(targets[i])) { continue; } var wrapperContent = targets[i].textContent; if (/^(<iframe src)/.test(wrapperContent) === false) { if (targets[i].children.length > 0) { var preamble = targets[i].children[0].textContent; var content = targets[i].children[1].textContent; targets[i].innerHTML = ""; var notebook = RunKit.createNotebook({ element: targets[i], source: content, preamble: preamble }); } } } }; function dynamicallyLoadRunkitLibrary() { if (typeof(dynamicallyLoadScript) === "undefined") return dynamicallyLoadScript("//embed.runkit.com") } activateRunkitTags(); </script> <div class="js-billboard-container pb-4 crayons-layout__comments-billboard" data-async-url="/jamiescript/gas-saving-techniques-in-solidity-324c/billboards/post_fixed_bottom"></div> <div id="runtime-banner-container"></div> </div> </div> <footer id="footer" class="crayons-footer print-hidden"> <div id="footer-container" class="crayons-footer__container"> <div style="" data-display-unit data-id="146443" data-category-click="click" data-category-impression="impression" data-context-type="home" data-special="nothing" data-article-id="126502" data-type-of="in_house"> <p style="font-weight: bold;margin-bottom: 10px"> Thank you to our Diamond Sponsor <a href="https://neon.tech/">Neon</a> for supporting our community. </p> </div> <p class="fs-s crayons-footer__description"> <a class="c-link c-link--branded fw-medium" aria-label="DEV Community Home" href="/">DEV Community</a> — A constructive and inclusive social network for software developers. With you every step of your journey. </p> <ul class="footer__nav-links flex gap-2 justify-center flex-wrap fs-s p-0" style="" /> <li class="footer__nav-link flex items-center"> <a href="/"> Home </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/++"> DEV++ </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/pod"> Podcasts </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/videos"> Videos </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/tags"> Tags </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/help"> DEV Help </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="https://shop.forem.com/"> Forem Shop </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/advertise"> Advertise on DEV </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/challenges"> DEV Challenges </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/showcase"> DEV Showcase </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/about"> About </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/contact"> Contact </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/free-postgres-database-tier"> Free Postgres Database </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/guides"> Guides </a> <span class="dot ml-2"></span> </li> </ul> <ul class="footer__nav-links flex gap-2 justify-center flex-wrap fs-s p-0" style="" /> <li class="footer__nav-link flex items-center"> <a href="/code-of-conduct"> Code of Conduct </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/privacy"> Privacy Policy </a> <span class="dot ml-2"></span> </li> <li class="footer__nav-link flex items-center"> <a href="/terms"> Terms of use </a> <span class="dot ml-2"></span> </li> </ul> <div class="fs-s"> <p>Built on <a class="c-link c-link--branded" target="_blank" rel="noopener" href="https://www.forem.com">Forem</a> — the <a target="_blank" rel="noopener" class="c-link c-link--branded" href="https://dev.to/t/opensource">open source</a> software that powers <a target="_blank" rel="noopener" class="c-link c-link--branded" href="https://dev.to">DEV</a> and other inclusive communities.</p> <p>Made with love and <a target="_blank" rel="noopener" class="c-link c-link--branded" href="https://dev.to/t/rails">Ruby on Rails</a>. DEV Community <span title="copyright">&copy;</span> 2016 - 2025.</p> </div> </div> </footer> <div id="snack-zone"></div> <div id="global-signup-modal" class="authentication-modal hidden"> <div class="authentication-modal__container"> <figure class="authentication-modal__image-container"> <img class="authentication-modal__image" src="https://media2.dev.to/dynamic/image/width=190,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8j7kvp660rqzt99zui8e.png" alt="DEV Community" loading="lazy" /> </figure> <div class="authentication-modal__content"> <p class="authentication-modal__description"> We&#39;re a place where coders share, stay up-to-date and grow their careers. </p> </div> <div class="authentication-modal__actions"> <a href="/enter" class="crayons-btn" aria-label="Log in" data-no-instant> Log in </a> <a href="/enter?state=new-user" class="crayons-btn crayons-btn--ghost-brand js-global-signup-modal__create-account" aria-label="Create new account" data-no-instant> Create account </a> </div> </div> </div> <script src="https://assets.dev.to/assets/signupModalShortcuts-0b25469b985100a01e94cbd7fccaf7f0a4d776e129aac65c766aa32cb28ab29a.js" defer="defer"></script> <div id="cookie-consent"></div> <div id="i18n-translations" data-translations="{&quot;en&quot;:{&quot;core&quot;:{&quot;add_comment&quot;:&quot;Add comment&quot;,&quot;beta&quot;:&quot;beta&quot;,&quot;comment&quot;:&quot;Comment&quot;,&quot;copy_link&quot;:&quot;Copy link&quot;,&quot;edit_profile&quot;:&quot;Edit profile&quot;,&quot;follow&quot;:&quot;Follow&quot;,&quot;follow_back&quot;:&quot;Follow back&quot;,&quot;following&quot;:&quot;Following&quot;,&quot;like&quot;:&quot;Like&quot;,&quot;loading&quot;:&quot;loading...&quot;,&quot;reaction&quot;:&quot;Reaction&quot;,&quot;report_abuse&quot;:&quot;Report abuse&quot;,&quot;search&quot;:&quot;Search&quot;,&quot;success_settings&quot;:&quot;Successfully updated settings.&quot;,&quot;counted_organization&quot;:{&quot;one&quot;:&quot;%{count} organization&quot;,&quot;other&quot;:&quot;%{count} organizations&quot;},&quot;counted_user&quot;:{&quot;one&quot;:&quot;%{count} person&quot;,&quot;other&quot;:&quot;%{count} people&quot;},&quot;not_following&quot;:&quot;You&#39;re not following anyone&quot;,&quot;following_everyone&quot;:&quot;You&#39;re following %{details} (everyone)&quot;,&quot;you_are_following&quot;:&quot;You&#39;re following&quot;,&quot;and&quot;:&quot;and&quot;}}}"></div> <div id="reaction-category-resources" class="hidden" aria-hidden="true"> <img data-name="Like" data-slug="like" data-position="1" src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18" /> <img data-name="Unicorn" data-slug="unicorn" data-position="2" src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" width="18" height="18" /> <img data-name="Exploding Head" data-slug="exploding_head" data-position="3" src="https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg" width="18" height="18" /> <img data-name="Raised Hands" data-slug="raised_hands" data-position="4" src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" width="18" height="18" /> <img data-name="Fire" data-slug="fire" data-position="5" src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="18" height="18" /> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10