Skip to content

Instantly share code, notes, and snippets.

@mds1
Last active May 9, 2023 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mds1/5814df610df021d23ffe25b2b3de0e66 to your computer and use it in GitHub Desktop.
Save mds1/5814df610df021d23ffe25b2b3de0e66 to your computer and use it in GitHub Desktop.
`abi.encodePacked` vs. `string.concat` (optimizer on, 200 runs, no via-ir) — bytecode of test files was also identical
$ forge test -vvv
[⠢] Compiling...
[⠒] Compiling 1 files with 0.8.19
[⠆] Solc 0.8.19 finished in 887.23ms
Compiler run successful!
Running 1 test for test/Counter.t.sol:TestShort_EncodePacked
[PASS] test1() (gas: 4373)
Logs:
462
helloworld
Test result: ok. 1 passed; 0 failed; finished in 328.75µs
Running 1 test for test/Counter.t.sol:TestShort_StringConcat
[PASS] test1() (gas: 4373)
Logs:
462
helloworld
Test result: ok. 1 passed; 0 failed; finished in 328.71µs
Running 1 test for test/Counter.t.sol:TestLong_StringConcat
[PASS] test1() (gas: 5032)
Logs:
745
hello hello hello hello hello hello hello hello hello hello hello hello hello hello helloworld world world world world world world world world world world world world world world
Test result: ok. 1 passed; 0 failed; finished in 277.92µs
Running 1 test for test/Counter.t.sol:TestLong_EncodePacked
[PASS] test1() (gas: 5032)
Logs:
745
hello hello hello hello hello hello hello hello hello hello hello hello hello hello helloworld world world world world world world world world world world world world world world
Test result: ok. 1 passed; 0 failed; finished in 339.21µs
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "forge-std/Test.sol";
contract TestShort_EncodePacked is Test {
function test1() public view {
string memory x = "hello";
string memory y = "world";
uint256 startGas = gasleft();
string memory z = string(abi.encodePacked(x, y));
console2.log(startGas - gasleft());
console2.log(z);
}
}
contract TestShort_StringConcat is Test {
function test1() public view {
string memory x = "hello";
string memory y = "world";
uint256 startGas = gasleft();
string memory z = string.concat(x, y);
console2.log(startGas - gasleft());
console2.log(z);
}
}
contract TestLong_EncodePacked is Test {
function test1() public view {
string memory x = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello";
string memory y = "world world world world world world world world world world world world world world world";
uint256 startGas = gasleft();
string memory z = string(abi.encodePacked(x, y));
console2.log(startGas - gasleft());
console2.log(z);
}
}
contract TestLong_StringConcat is Test {
function test1() public view {
string memory x = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello";
string memory y = "world world world world world world world world world world world world world world world";
uint256 startGas = gasleft();
string memory z = string.concat(x, y);
console2.log(startGas - gasleft());
console2.log(z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment