Skip to content

Commit 4a3c455

Browse files
authored
Merge pull request #14 from jf-tech/bench
add minify json helpers to jsons package and add timetrie initiailization benchmark (so that it won't become too slow before too late)
2 parents 3193a35 + 81dcd98 commit 4a3c455

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

jsons/jsons.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,28 @@ func BestEffortPrettyJSON(jsonStr string) string {
6565

6666
// BPJ is a shortcut (mostly used in tests) to BestEffortPrettyJSON.
6767
var BPJ = BestEffortPrettyJSON
68+
69+
// BestEffortMinifyMarshal returns a mninified JSON encoding of v. Any error countered will cause "{}" be returned.
70+
func BestEffortMinifyMarshal(v interface{}) string {
71+
b, err := json.Marshal(v)
72+
if err != nil {
73+
return "{}"
74+
}
75+
return string(b)
76+
}
77+
78+
// BMM is a shortcut to BestEffortMinifyMarshal
79+
var BMM = BestEffortMinifyMarshal
80+
81+
// BestEffortMinifyJSON reformats a json string to be minimal, ignoring any error.
82+
func BestEffortMinifyJSON(jsonStr string) string {
83+
var v interface{}
84+
err := json.Unmarshal([]byte(jsonStr), &v)
85+
if err != nil {
86+
return "{}"
87+
}
88+
return BestEffortMinifyMarshal(v)
89+
}
90+
91+
// BMJ is a shortcut to BestEffortMinifyJSON
92+
var BMJ = BestEffortMinifyJSON

jsons/jsons_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,25 @@ func TestBPJ_Success(t *testing.T) {
126126
pretty := BPJ(`{"Name":"John","Age":65,"Smoker":false,"Hobbies":["flying","fishing","reading"]}`)
127127
cupaloy.SnapshotT(t, pretty)
128128
}
129+
130+
func TestBestEffortMinifyMarshal(t *testing.T) {
131+
assert.Equal(t,
132+
`{"a":1,"b":false,"c":null,"d":3.14}`,
133+
BestEffortMinifyMarshal(map[string]interface{}{"a": 1, "b": false, "c": nil, "d": 3.14}))
134+
assert.Equal(t,
135+
`{}`,
136+
BMM(struct{ Unmarshallable func() }{nil}))
137+
}
138+
139+
func TestBestEffortMinifyJSON(t *testing.T) {
140+
assert.Equal(t,
141+
`{"a":1,"b":false,"c":null,"d":3.14}`,
142+
BestEffortMinifyJSON(`
143+
{
144+
"a": 1,
145+
"b": false,
146+
"c": null,
147+
"d": 3.14
148+
}`))
149+
assert.Equal(t, `{}`, BMJ("invalid json"))
150+
}

times/timetrie.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ var dateTimeTrie *strs.RuneTrie
243243
var allTimezones map[string]bool
244244

245245
func init() {
246+
// These two initialization routes take in total at about 10s of milliseconds. See benchmark in test.
246247
dateTimeTrie = initDateTimeTrie()
247248
allTimezones = initTimezones()
248249
}

times/timetrie_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ func TestInitTimezones(t *testing.T) {
3131
t.Logf("total timezones: %d", len(m))
3232
cupaloy.SnapshotT(t, jsons.BPM(m))
3333
}
34+
35+
// BenchmarkInitDateTimeTrie-8 100 18147722 ns/op 6505165 B/op 129455 allocs/op
36+
func BenchmarkInitDateTimeTrie(b *testing.B) {
37+
for i := 0; i < b.N; i++ {
38+
_ = initDateTimeTrie()
39+
}
40+
}
41+
42+
// BenchmarkInitTimezones-8 10000 109394 ns/op 45532 B/op 24 allocs/op
43+
func BenchmarkInitTimezones(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
_ = initTimezones()
46+
}
47+
}

0 commit comments

Comments
 (0)