Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

perf: optimize account init logging #4318

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/packages/cli/src/initialize/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) {
const liveOptions = provider.getOptions();
const accounts = provider.getInitialAccounts();

const addresses = Object.keys(accounts);
const addresses = Object.entries(accounts);
const logs = [];
logs.push("");
logs.push("Available Accounts");
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
logs.push("==================");
if (addresses.length > 0) {
addresses.forEach(function (address, index) {
const balance = accounts[address].balance;
addresses.forEach(([address, account], index) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checkout out this for benchmark: https://github.com/LeanyLabs/js-array-performance and reran with node 20.2.0 and found that forEach is even more slow compared with for of than it was in node 14.

Array.forEach x 130 ops/sec ±0.22% (84 runs sampled)
for of x 529 ops/sec ±0.57% (94 runs sampled)

While we are here, do you think it's worthwhile updating these Array.forEachs to for....ofs?

const balance = account.balance;
const strBalance = balance / WEI;
const about = balance % WEI === 0n ? "" : "~";
let line = `(${index}) ${toChecksumAddress(
address
)} (${about}${strBalance} ETH)`;

if (!accounts[address].unlocked) {
if (!account.unlocked) {
line += " 🔒";
}

Expand All @@ -41,8 +41,8 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) {
logs.push("Private Keys");
logs.push("==================");

addresses.forEach(function (address, index) {
logs.push(`(${index}) ${accounts[address].secretKey}`);
addresses.forEach(([_, account], index) => {
logs.push(`(${index}) ${account.secretKey}`);
});

if (liveOptions.wallet.accountKeysPath != null) {
Expand Down