Skip to content

Commit

Permalink
Shortcut when two json documents are same (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Kenjiro Nakayama <nakayamakenjiro@gmail.com>
  • Loading branch information
nak3 committed Aug 14, 2023
1 parent 13a9e4a commit 17d7994
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions v2/bench_test.go
Expand Up @@ -19,8 +19,8 @@ func BenchmarkCreatePatch(b *testing.B) {
},
{
"large array",
largeArray(1000),
largeArray(1000),
largeArray(1000, "a"),
largeArray(1000, "b"),
},
{
"simple",
Expand All @@ -40,7 +40,7 @@ func BenchmarkCreatePatch(b *testing.B) {
}
}

func largeArray(size int) string {
func largeArray(size int, val string) string {
type nested struct {
A, B string
}
Expand All @@ -49,7 +49,7 @@ func largeArray(size int) string {
}
a := example{}
for i := 0; i < size; i++ {
a.Objects = append(a.Objects, nested{A: "a", B: "b"})
a.Objects = append(a.Objects, nested{A: "a", B: val})
}
res, _ := json.Marshal(a)
return string(res)
Expand Down
4 changes: 4 additions & 0 deletions v2/jsonpatch.go
@@ -1,6 +1,7 @@
package jsonpatch

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -64,6 +65,9 @@ func NewOperation(op, path string, value interface{}) Operation {
//
// An error will be returned if any of the two documents are invalid.
func CreatePatch(a, b []byte) ([]Operation, error) {
if bytes.Equal(a, b) {
return []Operation{}, nil
}
var aI interface{}
var bI interface{}
err := json.Unmarshal(a, &aI)
Expand Down

0 comments on commit 17d7994

Please sign in to comment.