Add performance improvements in serializeFields function#123
Open
mkszepp wants to merge 1 commit into
Open
Conversation
Owner
|
Amazing! Will definitely take a look at these soon. Thanks for your investigation :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recently, I started using
"tobyz/json-api-server": "1.0.0-rc.1"in an app.During my first integrations, I checked the performance of JSON responses (without pagination) with larger result sets (1000+ items).
While I know that pagination is the recommended pattern, I still think it could be interesting to improve some performance issues — especially because these optimizations would also benefit users with smaller result sets.
Here are my test results:
Using a list with 3336 data items and one include (which returns 957 included items), the response time is currently around 1.2–1.4 seconds on my development machine. Query execution time is excluded from this measurement (additional SQL-related work is only around ~100ms). This means the total response time is around 1.5 seconds.
On the production server, the same API call takes around 2 seconds with the same dataset.
My conclusion after benchmarking was that the bottleneck is inside this package. So I started investigating possible causes and found the following:
Fix 1
Calling
->withField(...)or->withInclude(...)clones the full object each time, which adds noticeable overhead.If this logic is combined into a single function, we can reduce one clone operation per field/include call.
This change improved performance by around ~50–100ms in my tests (not huge, but still something 😅).
Fix 2
In the same area, I noticed an
in_arraycheck without$strict = true. This means every comparison uses==instead of===.After switching to strict comparisons, I was able to significantly improve performance. In my app, this change reduced response time by around ~500–700ms.
Conclusion
The API response time dropped from ~1.3 seconds to ~750ms — nearly half the original time.
Additional Benchmark
I also tested another API endpoint with 4177 items and 3336 included records.
Question
Is there any way to bring these fixes into the package?
I think Fix 1 should be safe for everyone.
For Fix 2, I’m not completely sure whether switching to strict comparisons could be breaking for some users. As an alternative, I’d also be fine with making strict comparison configurable via an option.