Skip to content
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

Reduce object creation in parsing #6750

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ module.exports = DatasetController.extend({
* @private
*/
_parse: function(start, count) {
var data = this.getDataset().data;
var metaData = this.getMeta().data;
const data = this.getDataset().data;
const meta = this.getMeta();
var i, ilen;
for (i = start, ilen = start + count; i < ilen; ++i) {
metaData[i]._val = +data[i];
meta._parsed[i] = +data[i];
}
},

Expand Down Expand Up @@ -232,7 +232,8 @@ module.exports = DatasetController.extend({
var centerY = (chartArea.top + chartArea.bottom) / 2;
var startAngle = opts.rotation; // non reset case handled later
var endAngle = opts.rotation; // non reset case handled later
var circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(arc._val * opts.circumference / DOUBLE_PI);
var meta = me.getMeta();
var circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(meta._parsed[index] * opts.circumference / DOUBLE_PI);
var innerRadius = reset && animationOpts.animateScale ? 0 : me.innerRadius;
var outerRadius = reset && animationOpts.animateScale ? 0 : me.outerRadius;
var options = arc._options || {};
Expand Down Expand Up @@ -271,16 +272,18 @@ module.exports = DatasetController.extend({
},

calculateTotal: function() {
var metaData = this.getMeta().data;
var meta = this.getMeta();
var metaData = meta.data;
var total = 0;
var value;
var i, ilen, arc, value;

helpers.each(metaData, function(arc) {
value = arc ? arc._val : NaN;
for (i = 0, ilen = metaData.length; i < ilen; i++) {
arc = metaData[i];
value = arc ? meta._parsed[i] : NaN;
if (!isNaN(value) && !arc.hidden) {
total += Math.abs(value);
}
});
}

/* if (total === 0) {
total = NaN;
Expand Down
3 changes: 2 additions & 1 deletion src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
xAxisID: null,
yAxisID: null,
order: dataset.order || 0,
index: datasetIndex
index: datasetIndex,
_parsed: []
};
}

Expand Down
22 changes: 10 additions & 12 deletions src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ helpers.extend(DatasetController.prototype, {
var me = this;
var type = me.dataElementType;
return type && new type({
_ctx: me.chart.ctx,
_parsed: {}
_ctx: me.chart.ctx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like createMetaDataset and createMetaData could be replaced with createMeta(type)

});
},

Expand Down Expand Up @@ -493,7 +492,7 @@ helpers.extend(DatasetController.prototype, {

for (i = 0, ilen = parsed.length; i < ilen; ++i) {
item = parsed[i];
meta.data[start + i]._parsed = item;
meta._parsed[start + i] = item;

if (stacks) {
item._stackKeys = {};
Expand Down Expand Up @@ -595,11 +594,11 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
_getParsed: function(index) {
var data = this._cachedMeta.data;
var data = this._cachedMeta._parsed;
if (index < 0 || index >= data.length) {
return;
}
return data[index]._parsed;
return data[index];
},

/**
Expand Down Expand Up @@ -636,7 +635,7 @@ helpers.extend(DatasetController.prototype, {

for (i = 0; i < ilen; ++i) {
item = metaData[i];
parsed = item._parsed;
parsed = meta._parsed[i];
value = parsed[scale.id];
otherValue = parsed[otherScale.id];
if (item.hidden || isNaN(value) ||
Expand Down Expand Up @@ -667,13 +666,12 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
_getAllParsedValues: function(scale) {
var meta = this._cachedMeta;
var metaData = meta.data;
var values = [];
const parsed = this._cachedMeta._parsed;
const values = [];
var i, ilen, value;

for (i = 0, ilen = metaData.length; i < ilen; ++i) {
value = metaData[i]._parsed[scale.id];
for (i = 0, ilen = parsed.length; i < ilen; ++i) {
value = parsed[i][scale.id];
if (!isNaN(value)) {
values.push(value);
}
Expand Down Expand Up @@ -969,10 +967,10 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
insertElements: function(start, count) {
this._parse(start, count);
for (var i = 0; i < count; ++i) {
this.addElementAndReset(start + i);
}
this._parse(start, count);
},

/**
Expand Down