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

Add setCurrent() method to Page Collection #3398

Merged
Merged
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions system/src/Grav/Common/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ public function intersect(PageCollectionInterface $collection)
return $this;
}

/**
* Set current page.
*/
public function setCurrent(string $path): void
{
reset($this->items);

while (key($this->items) !== $path && key($this->items) !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would get the key into a variable to speed up the loop.

Copy link
Contributor Author

@Karmalakas Karmalakas Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But key setting will have to be inside the loop anyway 🤔 Will see what I can come up with. Also will check how expensive this is (I know it might be 2x faster maybe, but really doubt there will be a significant difference)

Copy link
Contributor Author

@Karmalakas Karmalakas Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth introducing a variable to gain 250ms in a 1M items collection? 🙂 Ran a few times and one time result was opposite even 🙂
image

Copy link
Member

@mahagr mahagr Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 9% faster. :) Generally I prefer well-written code over repeating the same thing over and over again.

while (($key = key($this->items)) !== null && $key !== $path) {
    next($this->items);
}

next($this->items);
}
}

/**
* Returns current page.
*
Expand Down