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

Optimize isList #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kamil-tekiela
Copy link

This is a follow up to the discussion from #247.
During normal usage, I noticed this function is very inefficient. It doesn't use the built-in function array_is_list and the polyfil is very inefficient for most cases. Yes, the current implementation is very efficient for true lists, but has very bad performance with any other list, e.g. the one from the previous PR.

The current implementation would benefit from a very simple optimization: foreach (array_keys($array) as $k) { but no sense in doing this if we can achieve an overall better performance with just array_keys.

The current implementation builds a new array in memory. This is super efficient if the array was built like this range(1, 1000). But if the array is large and contains other arrays or objects then building a copy of it is very bad. So we need to get rid of array_values(). This also solves the issue from previous PR because we are no longer comparing the values.

This is the most efficient and the most correct implementation I could find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant