Skip to content

Commit

Permalink
fix: use strings.Builder
Browse files Browse the repository at this point in the history
In the ConvertBigIntArrayToString function, the big.Int is first converted to a string and then concatenated with commas. This can be optimized using strings.Builder, which avoids creating new string objects on each concatenation. The performance advantage is particularly significant when dealing with larger arrays.
  • Loading branch information
YESUNFEI committed Dec 11, 2024
1 parent 06beb5d commit 97688ef
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions bridge-history-api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ func GetBlocksInRange(ctx context.Context, cli *ethclient.Client, start, end uin

// ConvertBigIntArrayToString convert the big int array to string
func ConvertBigIntArrayToString(array []*big.Int) string {
stringArray := make([]string, len(array))
var sb strings.Builder
for i, num := range array {
stringArray[i] = num.String()
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(num.String())
}

result := strings.Join(stringArray, ", ")
return result
return sb.String()
}

// ConvertStringToStringArray takes a string with values separated by commas and returns a slice of strings
Expand Down

0 comments on commit 97688ef

Please sign in to comment.