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

Filter first, the the rest #47

Open
KES777 opened this issue Jan 28, 2025 · 3 comments
Open

Filter first, the the rest #47

KES777 opened this issue Jan 28, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@KES777
Copy link

KES777 commented Jan 28, 2025

@@ -270,10 +274,10 @@ class FormDataJson {
      * @return {*}
      */
     function output () {
+      if (options.skipEmpty) returnObject = removeEmpty(returnObject) || (options.flatList ? [] : {})
       if (options.arrayify) {
         returnObject = arrayify(returnObject)
       }
-      if (options.skipEmpty) returnObject = removeEmpty(returnObject) || (options.flatList ? [] : {})
       return returnObject
     }

This is more faster because the next modification functions need to work with less amount of elements.

@KES777 KES777 added the enhancement New feature or request label Jan 28, 2025
@brainfoolong
Copy link
Owner

It doesnt make a difference and is a micro optimization with no benefit. When you have both flags activated, there is no measurable improvement at all.

No need to change this, as it doesn't make any real world difference.

@KES777
Copy link
Author

KES777 commented Jan 29, 2025

It does.

Lets examine then this next real life case.
Eg. if we have 100 inputs and only one is filled, first the code above will try to arrayify 100 elements and then process another 100 items to filter out 99 of them.
In total 200 items were processed.

In case if this patch will be applied. Those 99 items will be filtered first and 1 left.
Then this 1 item will be 'arrayify'ed.
In total 101 items were processed in this case.

If optimized it becomes twice! efficient.

  1. Example.
    Very often HTML forms has hidden fields, which are never visible to user and, probably, not filled. Thus it would be nice to filter out these hidden fields before attempting to arrayify them.

In my case I have:

  Company[Phone][0][id]
  Company[Phone][0][phone]
  Company[Phone][1][id]
  Company[Phone][1][phone]
  Company[Phone][X][id]
  Company[Phone][X][phone]

Where [X] inputs are hidden and could be cloned as many times as user want to add more phones to his company. These cloned fields become [2], [3] and so on.

Otherwise when form if submitted arrayify will fail, because X is not number and could not be parsed.

Probably the last example is just my corner case, but with first example you can see that with optimization code works twice! faster.

Thanks.

@brainfoolong
Copy link
Owner

You are right, it was my head wrongly wrapped around. I'll someday implement this.

@brainfoolong brainfoolong reopened this Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants