Permalink
Browse files

make array comparison faster

in array comparison, if match has been found - no need to proceed
further - break at that point and proceed with next element
  • Loading branch information...
edast committed Jul 27, 2017
1 parent d220c21 commit 7f6a70dff97e64594210286d6383b21314e17e84
Showing with 32 additions and 1 deletion.
  1. +2 −0 jsonpatch.go
  2. +30 −1 jsonpatch_simple_test.go
View
@@ -220,6 +220,7 @@ func compareArray(av, bv []interface{}, p string) []JsonPatchOperation {
for _, v2 := range bv {
if reflect.DeepEqual(v, v2) {
found = true
break
}
}
if !found {
@@ -232,6 +233,7 @@ func compareArray(av, bv []interface{}, p string) []JsonPatchOperation {
for _, v2 := range av {
if reflect.DeepEqual(v, v2) {
found = true
break
}
}
if !found {
View
@@ -1,9 +1,10 @@
package jsonpatch
import (
"github.com/stretchr/testify/assert"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var simpleA = `{"a":100, "b":200, "c":"hello"}`
@@ -89,3 +90,31 @@ func TestVsEmpty(t *testing.T) {
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/c", "they should be equal")
}
func BenchmarkBigArrays(b *testing.B) {
var a1, a2 []interface{}
a1 = make([]interface{}, 100)
a2 = make([]interface{}, 101)
for i := 0; i < 100; i++ {
a1[i] = i
a2[i+1] = i
}
for i := 0; i < b.N; i++ {
compareArray(a1, a2, "/")
}
}
func BenchmarkBigArrays2(b *testing.B) {
var a1, a2 []interface{}
a1 = make([]interface{}, 100)
a2 = make([]interface{}, 101)
for i := 0; i < 100; i++ {
a1[i] = i
a2[i] = i
}
for i := 0; i < b.N; i++ {
compareArray(a1, a2, "/")
}
}

0 comments on commit 7f6a70d

Please sign in to comment.