Skip to content

Commit 17d7994

Browse files
authored
Shortcut when two json documents are same (#37)
Signed-off-by: Kenjiro Nakayama <[email protected]>
1 parent 13a9e4a commit 17d7994

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

v2/bench_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func BenchmarkCreatePatch(b *testing.B) {
1919
},
2020
{
2121
"large array",
22-
largeArray(1000),
23-
largeArray(1000),
22+
largeArray(1000, "a"),
23+
largeArray(1000, "b"),
2424
},
2525
{
2626
"simple",
@@ -40,7 +40,7 @@ func BenchmarkCreatePatch(b *testing.B) {
4040
}
4141
}
4242

43-
func largeArray(size int) string {
43+
func largeArray(size int, val string) string {
4444
type nested struct {
4545
A, B string
4646
}
@@ -49,7 +49,7 @@ func largeArray(size int) string {
4949
}
5050
a := example{}
5151
for i := 0; i < size; i++ {
52-
a.Objects = append(a.Objects, nested{A: "a", B: "b"})
52+
a.Objects = append(a.Objects, nested{A: "a", B: val})
5353
}
5454
res, _ := json.Marshal(a)
5555
return string(res)

v2/jsonpatch.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsonpatch
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"reflect"
@@ -64,6 +65,9 @@ func NewOperation(op, path string, value interface{}) Operation {
6465
//
6566
// An error will be returned if any of the two documents are invalid.
6667
func CreatePatch(a, b []byte) ([]Operation, error) {
68+
if bytes.Equal(a, b) {
69+
return []Operation{}, nil
70+
}
6771
var aI interface{}
6872
var bI interface{}
6973
err := json.Unmarshal(a, &aI)

0 commit comments

Comments
 (0)