Speed improvements #486
Speed improvements #486
Changes from all commits
134db97
81832ec
0e582c3
3734b60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,11 @@ treeherder.factory('ThJobModel', [ | |
// ThJobModel is the js counterpart of job | ||
|
||
var ThJobModel = function(data) { | ||
// creates a new instance of ThJobModel | ||
// using the provided properties | ||
angular.extend(this, data); | ||
if (typeof data === "object" && data !== null) { | ||
for (var k in data) { | ||
this[k] = data[k]; | ||
} | ||
} | ||
}; | ||
|
||
ThJobModel.prototype.get_current_eta = function(){ | ||
|
@@ -55,19 +57,30 @@ treeherder.factory('ThJobModel', [ | |
if(_.has(response.data, 'job_property_names')){ | ||
// the results came as list of fields | ||
//we need to convert them to objects | ||
item_list = _.map(response.data.results, function(elem){ | ||
var job_obj = _.object(response.data.job_property_names, elem); | ||
return new ThJobModel(job_obj); | ||
}); | ||
item_list = []; | ||
var props = response.data.job_property_names; | ||
var propsLen = props.length; | ||
for (var i = 0, resultsLen = response.data.results.length; i < propsLen; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition to break the loop should be i < resultsLen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before breaking this stuff out into loops (and making the code much less readable), I wonder if we should consider using lodash (https://lodash.com/) instead of underscore. It's supposed to be quite a bit faster than underscore (http://benmccormick.org/2014/11/12/underscore-vs-lodash/), and indeed uses the technique you're using here to iterate through arrays with map (and other things): https://github.com/lodash/lodash/blob/master/lodash.js#L1443 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes! Thanks! All passing now. |
||
var model = new ThJobModel(); | ||
var elem = response.data.results[i]; | ||
for (var j = 0; j < propsLen; j++) { | ||
model[props[j]] = elem[j]; | ||
} | ||
item_list.push(model); | ||
} | ||
}else{ | ||
item_list = _.map(response.data.results, function(job_obj){ | ||
return new ThJobModel(job_obj); | ||
}); | ||
} | ||
|
||
// next_pages_jobs is wrapped in a $q.when call because it could be | ||
// either a promise or a value | ||
return $q.when(next_pages_jobs).then(function(maybe_job_list){ | ||
return item_list.concat(maybe_job_list); | ||
return $q.when(next_pages_jobs).then(function(maybe_job_list) { | ||
for (var i = 0, n = maybe_job_list.length; i < n; i++) { | ||
item_list.push(maybe_job_list[i]); | ||
} | ||
return item_list; | ||
}) | ||
}); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a comment here to explain why we're not using angular.extend, otherwise someone might be tempted to change it back (since the original form is more concise).