Skip to content

improve performance of tostring on integers and json.Number - #340

Merged
itchyny merged 3 commits into
itchyny:mainfrom
g4lb:perf/tostring-int
Nov 22, 2026
Merged

itchyny merged 3 commits into
itchyny:mainfrom
g4lb:perf/tostring-int

Conversation

@g4lb

@g4lb g4lb commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

toString used the JSON encoder for all values that are not strings. Now integers use strconv.Itoa, and a json.Number returns its string. The output does not change. This helps tostring, string interpolation and @text, also in the CLI, which decodes numbers to json.Number.

Timings for 200 values (go1.27.4, linux/amd64):

Query Input Before After Allocs
[.[] | tostring] json.Number 38.3 µs 23.4 µs 1220 → 620
[.[] | "id=\(.)"] json.Number 55.4 µs 42.4 µs 1621 → 1021
[.[] | tostring] int 39.5 µs 27.4 µs 1220 → 819
[.[] | @text] int 40.2 µs 28.0 µs 1220 → 819

toString is no longer inlined. Strings and floats are about 1% slower.

Comment thread func.go

// numberToString formats integers and json.Number without the encoder.
func numberToString(v any) string {
switch v := v.(type) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This type switch should be folded with the string check in toString.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done in 87cb53b.

Comment thread func.go Outdated
return numberToString(v)
}

// numberToString formats integers and json.Number without the encoder.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

numberToString is inappropriate naming because this stringify all types. just inline and remove this name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done in 87cb53b.

@g4lb
g4lb requested a review from itchyny November 22, 2026 08:00
Comment thread func.go
case string:
return v
case int:
return strconv.Itoa(v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If this function handles int, should handle *big.Int, too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done in b493ddc.

@g4lb
g4lb requested a review from itchyny November 22, 2026 09:30

@itchyny itchyny left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you.

@itchyny
itchyny merged commit cb57441 into itchyny:main Nov 22, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants