Skip to content

Commit c71b41e

Browse files
committed
performance optimization
1 parent b0bc1ba commit c71b41e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/JsonDiff.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class JsonDiff
66
{
77
const SKIP_REARRANGE_ARRAY = 1;
8+
const STOP_ON_DIFF = 2;
89

910
private $options = 0;
1011
private $original;
@@ -42,6 +43,15 @@ public function __construct($original, $new, $options = 0)
4243
$this->rearranged = $this->rearrange();
4344
}
4445

46+
/**
47+
* Returns total number of differences
48+
* @return int
49+
*/
50+
public function getDiffCnt()
51+
{
52+
return $this->addedCnt + $this->modifiedCnt + $this->removedCnt;
53+
}
54+
4555
/**
4656
* Returns removals as partial value of original.
4757
* @return mixed
@@ -157,6 +167,9 @@ private function process($original, $new)
157167
$this->modifiedPaths [] = $this->path;
158168
JsonProcessor::pushByPath($this->modifiedOriginal, $this->path, $original);
159169
JsonProcessor::pushByPath($this->modifiedNew, $this->path, $new);
170+
if ($this->options & self::STOP_ON_DIFF) {
171+
return;
172+
}
160173
}
161174
return $new;
162175
}
@@ -184,6 +197,9 @@ private function process($original, $new)
184197
$this->removedCnt++;
185198
$this->removedPaths [] = $this->path;
186199
JsonProcessor::pushByPath($this->removed, $this->path, $originalValue);
200+
if ($this->options & self::STOP_ON_DIFF) {
201+
return;
202+
}
187203
}
188204
$this->path = $path;
189205
}
@@ -195,6 +211,9 @@ private function process($original, $new)
195211
JsonProcessor::pushByPath($this->added, $path, $value);
196212
$this->addedCnt++;
197213
$this->addedPaths [] = $path;
214+
if ($this->options & self::STOP_ON_DIFF) {
215+
return;
216+
}
198217
}
199218

200219
return is_array($new) ? $newOrdered : (object)$newOrdered;

0 commit comments

Comments
 (0)