iancoleman / orderedmap

orderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.
MIT License
360 stars 55 forks source link

Unmarshal using token decoder #19

Closed itchyny closed 3 years ago

itchyny commented 3 years ago

I fixed the unmarshaler to use decoder.Token() to fix #17. Also improves the performance (closes #2).

BenchmarkUnmarshalJSON ```go func BenchmarkUnmarshalJSON(b *testing.B) { s := `{ "number": 4, "string": "x", "z": 1, "a": "should not break with unclosed { character in value", "b": 3, "slice": [ "1", 1 ], "orderedmap": { "e": 1, "a { nested key with brace": "with a }}}} }} {{{ brace value", "after": { "link": "test {{{ with even deeper nested braces }" } }, "test\"ing": 9, "after": 1, "multitype_array": [ "test", 1, { "map": "obj", "it" : 5, ":colon in key": "colon: in value" }, [{"inner": "map"}] ], "should not break with { character in key": 1 }` for i := 0; i < b.N; i++ { o := New() err := json.Unmarshal([]byte(s), &o) if err != nil { b.Error("JSON Unmarshal error", err) } } } ```
benchmark                     old ns/op     new ns/op     delta
BenchmarkUnmarshalJSON-16     125485        37536         -70.09%

benchmark                     old allocs     new allocs     delta
BenchmarkUnmarshalJSON-16     964            389            -59.65%

benchmark                     old bytes     new bytes     delta
BenchmarkUnmarshalJSON-16     49885         13174         -73.59%
iancoleman commented 3 years ago

Thanks for this, much appreciated. The code is much cleaner using Token and the performance is much improved. Thanks again.