Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store shuffled alphabets so that they don't have to be calculated again. #105

Merged
merged 3 commits into from
Feb 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Hashids.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class Hashids implements HashidsInterface
*/
protected $alphabet;

/**
* Shuffled alphabets, referenced by alphabet and salt.
*
* @var array
*/
protected $shuffledAlphabets;

/**
* The seps string.
*
Expand Down Expand Up @@ -300,6 +307,12 @@ public function decodeHex($hash)
*/
protected function shuffle($alphabet, $salt)
{
$key = $alphabet.' '.$salt;

if (isset($this->shuffledAlphabets[$key])) {
return $this->shuffledAlphabets[$key];
}

$saltLength = strlen($salt);

if (!$saltLength) {
Expand All @@ -316,6 +329,8 @@ protected function shuffle($alphabet, $salt)
$alphabet[$i] = $temp;
}

$this->shuffledAlphabets[$key] = $alphabet;

return $alphabet;
}

Expand Down