-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Performance update for resolveFieldData #4290
Conversation
Object.keys can get expensive when called a lot of times. Adding a try catch for the quickest and probably highest use case can lead to big performance gains
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
Could you create a separate issue for this PR? |
#4296 Created |
Nice @MoMack20 keep em coming! I will make this change in PrimeReact. I agree with this change. |
@MoMack20 here is my final version in PrimeReact which short circuits the field or data being undefined. static resolveFieldData(data, field) {
if (!data || !field) {
// short circuit if there is nothing to resolve
return null;
}
try {
const value = data[field];
if (value) return value;
} catch {
// Performance optimization: https://github.com/primefaces/primereact/issues/4797
// do nothing and continue to other methods to resolve field data
}
if (Object.keys(data).length) {
if (this.isFunction(field)) {
return field(data);
} else if (ObjectUtils.isNotEmpty(data[field])) {
return data[field];
} else if (field.indexOf('.') === -1) {
return data[field];
} else {
let fields = field.split('.');
let value = data;
for (var i = 0, len = fields.length; i < len; ++i) {
if (value == null) {
return null;
}
value = value[fields[i]];
}
return value;
}
}
return null;
} |
@melloware let me know if you measure the actual gainz from this. I'm interested to see the real impact. |
@MoMack20 its minimal but it makes the code much more clean and readable. It prevents the JS engine from try catching the null pointer on data if data = null and field = "hello". But mostly for clean readability of "short circuiting". |
Thanks a lot for your contribution! |
Object.keys can get expensive when called a lot of times. Adding a try catch for the quickest and probably highest use case can lead to big performance gains. Related to #4296 but for overall performance
I don't have the time to run through the other scenarios, but I think just doing try catch for each scenario would be faster than the call to Object.keys and isFunction. It would look something like.
A screen showing the difference when using chrome tools performance tab
I imagine there are other places in the code that could also benefit from this type of improvement.