Browse Source

Couple of performance gains in data insertion.

newShading
Ludo Stellingwerff 8 years ago
parent
commit
88393739d9
2 changed files with 33 additions and 11 deletions
  1. +14
    -7
      lib/timeline/component/GraphGroup.js
  2. +19
    -4
      lib/timeline/component/LineGraph.js

+ 14
- 7
lib/timeline/component/GraphGroup.js View File

@ -30,6 +30,17 @@ function GraphGroup(group, groupId, options, groupsUsingDefaultStyles) {
}
function insertionSort (a,compare) {
for (var i = 0; i < a.length; i++) {
var k = a[i];
for (var j = i; j > 0 && compare(k,a[j - 1])<0; j--) {
a[j] = a[j - 1];
}
a[j] = k;
}
return a;
}
/**
* this loads a reference to all items in this group into this group.
* @param {array} items
@ -38,13 +49,9 @@ GraphGroup.prototype.setItems = function (items) {
if (items != null) {
this.itemsData = items;
if (this.options.sort == true) {
this.itemsData.sort(function (a, b) {
return a.x - b.x;
})
}
// typecast all items to numbers. Takes around 10ms for 500.000 items
for (var i = 0; i < this.itemsData.length; i++) {
this.itemsData[i].y = Number(this.itemsData[i].y);
insertionSort(this.itemsData,function (a, b) {
return a.x > b.x ? 1 : -1;
});
}
}
else {

+ 19
- 4
lib/timeline/component/LineGraph.js View File

@ -455,20 +455,35 @@ LineGraph.prototype._updateAllGroupData = function () {
if (this.itemsData != null) {
var groupsContent = {};
var items = this.itemsData.get();
//pre-Determine array sizes, for more efficient memory claim
var groupCounts = {};
for (var i = 0; i < items.length; i++) {
var item = items[i];
var groupId = item.group;
if (groupId === null || groupId === undefined) {
groupId = UNGROUPED;
}
if (groupsContent[groupId] === undefined) {
groupsContent[groupId] = [];
groupCounts.hasOwnProperty(groupId) ? groupCounts[groupId]++ : groupCounts[groupId] = 1;
}
//Now insert data into the arrays.
for (var i = 0; i < items.length; i++) {
var item = items[i];
var groupId = item.group;
if (groupId === null || groupId === undefined) {
groupId = UNGROUPED;
}
if (!groupsContent.hasOwnProperty(groupId)) {
groupsContent[groupId] = new Array(groupCounts[groupId]);
}
//Copy data (because of unmodifiable DataView input.
var extended = util.bridgeObject(item);
extended.x = util.convert(item.x, 'Date');
extended.orginalY = item.y; //real Y
extended.y = item.y;
groupsContent[groupId].push(extended);
// typecast all items to numbers. Takes around 10ms for 500.000 items
extended.y = Number(item.y);
var index= groupsContent[groupId].length - groupCounts[groupId]--;
groupsContent[groupId][index] = extended;
}
//Update legendas, style and axis
for (var groupId in groupsContent) {

Loading…
Cancel
Save