Skip to content

Commit 573572d

Browse files
committed
Cache unit lookups
Provided roughly a 10% performance boost when adding a large list of filesizes
1 parent bf3220f commit 573572d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/FileSize.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class FileSize
1313
*/
1414
private $bytes;
1515

16+
/**
17+
* A list of user filesize units previously mapped.
18+
*
19+
* @var array
20+
*/
21+
private $unitCache = [];
22+
1623
/**
1724
* A mapping of filesize units to lowercase strings.
1825
*
@@ -151,10 +158,15 @@ public function asAuto($precision = 2)
151158
*/
152159
private function lookupUnit($unitString)
153160
{
161+
if (isset($this->unitCache[$unitString])) {
162+
return $this->unitCache[$unitString];
163+
}
164+
154165
$lowerUnitString = strtolower($unitString);
155166

156167
foreach (self::$unitMap as $key => $list) {
157168
if (in_array($lowerUnitString, $list)) {
169+
$this->unitCache[$unitString] = $key;
158170
return $key;
159171
}
160172
}

tests/phpunit.xml

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
</testsuite>
1414
</testsuites>
1515

16+
<php>
17+
<!-- <ini name="memory_limit" value="256M" /> -->
18+
</php>
19+
1620
</phpunit>

0 commit comments

Comments
 (0)