-
Notifications
You must be signed in to change notification settings - Fork 76
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
Avoid using Function constructor. #26
Conversation
Using the Function constructor is not compatible with CSP without 'unsafe-eval' as it evals the passed code. Use a function literal instead.
This behavior is noted in the README along with the suggestion to use parseRows if a CSP is in place. Changing the implementation as proposed here had a substantial negative performance impact, at least last time I measured it, and so I am wary of changing the default behavior. If you want to verify that this behavior has been fixed in modern browsers for large input files, then I will reconsider this request. Thank you! |
Please give me instructions about how you want to measure this. |
I was able to reproduce the slowness in Chrome by parsing
|
I was able to bring it down with this code: function objectConverter(columns) {
var result = {};
return function(d) {
for (var i = 0; i < columns.length; i++) {
result[columns[i]] = d[i];
}
return result;
};
}
It's slightly faster in Firefox and slightly slower in Chrome. Is this acceptable for you? I've also updated the README which should appear if you re-open this pull request. |
That implementation returns the same object for every row rather than a new object for each row. |
You are right. |
I will not be able to make this fast. Thanks for your feedback. |
This is still biting us. People keep using this function and when we want to deploy CSP, we need to refactor. Would you accept a patch using the CSP-compatible version if CSP is enabled? I can use a try-catch around the This is a benchmark from today:
Also, this is so quick these days that maybe it's not worth optimizing anymore? |
Created #87. |
This is the original benchmark with this code: function objectConverter(columns) {
return function(d) {
var result = {};
for (var i = 0; i < columns.length; i++) {
result[columns[i]] = d[i] || '';
}
return result;
};
}
|
Using the Function constructor is not compatible with CSP without
'unsafe-eval' as it evals the passed code. Use a function literal
instead.