diff --git a/lib/timeline/component/GraphGroup.js b/lib/timeline/component/GraphGroup.js index 458fac61..c1326e65 100644 --- a/lib/timeline/component/GraphGroup.js +++ b/lib/timeline/component/GraphGroup.js @@ -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 { diff --git a/lib/timeline/component/LineGraph.js b/lib/timeline/component/LineGraph.js index 7a1d8cc9..52bd9ebd 100644 --- a/lib/timeline/component/LineGraph.js +++ b/lib/timeline/component/LineGraph.js @@ -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) {