Skip to content
Permalink
Browse files

refactor(Aggregation): Use local function

The function was always iterating all rows and saving the results on a local variable to be used in only 4/6 of the time, causing a lot of overhead.

Its now wrapped in a local function and is called only when needed.
  • Loading branch information
danielzabotti authored and c0bra committed Dec 2, 2014
1 parent a647c34 commit 47ac609ed5da97b78df31f781023590af9f1fb33
Showing with 20 additions and 15 deletions.
  1. +20 −15 src/js/core/factories/GridColumn.js
@@ -622,37 +622,42 @@ angular.module('ui.grid')
var self = this;
var result = 0;
var visibleRows = self.grid.getVisibleRows();
var cellValues = [];
angular.forEach(visibleRows, function (row) {
var cellValue = self.grid.getCellValue(row, self);
if (angular.isNumber(cellValue)) {
cellValues.push(cellValue);
}
});

var cellValues = function(){
var values = [];
angular.forEach(visibleRows, function (row) {
var cellValue = self.grid.getCellValue(row, self);
if (angular.isNumber(cellValue)) {
values.push(cellValue);
}
});
return values;
};

if (angular.isFunction(self.aggregationType)) {
return self.aggregationType(visibleRows, self);
}
else if (self.aggregationType === uiGridConstants.aggregationTypes.count) {
return self.grid.getVisibleRowCount();
return self.getAggregationText('aggregation.count', self.grid.getVisibleRowCount());
}
else if (self.aggregationType === uiGridConstants.aggregationTypes.sum) {
angular.forEach(cellValues, function (value) {
angular.forEach(cellValues(), function (value) {
result += value;
});
return result;
return self.getAggregationText('aggregation.sum', result);
}
else if (self.aggregationType === uiGridConstants.aggregationTypes.avg) {
angular.forEach(cellValues, function (value) {
angular.forEach(cellValues(), function (value) {
result += value;
});
result = result / cellValues.length;
return result;
result = result / cellValues().length;
return self.getAggregationText('aggregation.avg', result);
}
else if (self.aggregationType === uiGridConstants.aggregationTypes.min) {
return Math.min.apply(null, cellValues);
return self.getAggregationText('aggregation.min', Math.min.apply(null, cellValues()));
}
else if (self.aggregationType === uiGridConstants.aggregationTypes.max) {
return Math.max.apply(null, cellValues);
return self.getAggregationText('aggregation.max', Math.max.apply(null, cellValues()));
}
else {
return '\u00A0';

0 comments on commit 47ac609

Please sign in to comment.
You can’t perform that action at this time.