4 ms·
I noticed a possible issue with the code generation, which may cause the issues with excessive heap allocation that were reported: var code = 'return {\n';
by rb12345 14y ago
I noticed a possible issue with the code generation, which may cause the issues with excessive heap allocation that were reported:
var code = 'return {\n';
columns.forEach(function(column) {
code += '"' + column.name + '":' + 'parser.readColumnValue(),\n';
});
code += '};\n';
var parseRow = new Function('columns', 'parser', code);
Instead of performing so many appends to a single string, I would try using map() and then join() the resulting strings:
var code = ['return {\n',
columns.map(function(column) {
return '"' + column.name + '":' + 'parser.readColumnValue(),\n';
}).join(''),
'};\n'].join('');
var parseRow = new Function('columns', 'parser', code);
Of course, this depends on how often the parseRow function is created...
- mistercow 14y agoThat might be an improvement, but you probably wouldn't want to use Array.map, which generally has pretty terrible performance. See http://jsperf.com/map-vs-native-for-loop/5 http://jsperf.com/map-vs-native-for-loop/5
- rb12345 14y agoYes; the only reason for using Array.map() here is conciseness and similarity to the original code.