perf: optimize Error object creation in promise.js functions
#3169
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.
Summary
This merge request optimizes several functions in
promise.js, includingping(),execute(),query(), and other similar methods, by moving the creation ofErrorobjects inside their respective error handling blocks. This change ensures thatErrorobjects are instantiated only when actual errors occur, reducing unnecessary overhead during successful operations.Background and Motivation
Performance Issue:
Errorobjects were being created every time these functions were called, regardless of whether an error occurred.Performance Analysis:
Errorobjects only when needed reduces CPU usage and execution time.Errorobject instantiation is deferred until an actual error occurs.Changes Made
Modified Functions:
ping()execute()query()promise.jsexhibiting the same pattern.Optimization Details:
Moved the instantiation of
Errorobjects inside theif (err)blocks within each function.Ensured that
Errorobjects are only created when an error is present.Example change:
Benefits
Performance Improvement:
Errorobjects during successful operations.Scalability:
Best Practices Compliance:
Testing
Compatibility
Additional Notes
Conclusion
By optimizing the creation of
Errorobjects inping(),execute(),query(), and other functions withinpromise.js, this change enhances the performance and scalability of themysql2library. Avoiding unnecessary instantiation ofErrorobjects reduces CPU and memory overhead, leading to more efficient resource utilization, especially in applications with high transaction volumes.