You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had identified very slow performance using moment.js with realtime chart updating with large dataset. Every update is calls whole redraw and ticks recalculation and reformatting. I need to draw just 120 ticks on two hours with aggregated to seconds and I found highly un-optimized processing in scale.time.js:buildTicks.
The problem is, that there is a cycle for (var i = 1; i <= me.scaleSizeInUnits; ++i) { which 7200 times clone moment.js object and made calculations and formatting.
I think there should be as many calculations as needed just for drawing the chart. I found, when I didn't fully fill me.ticks array there was no problem with axis rendering except missing labels (every ticks was correctly positioned).
There should be some form of aggregation of ticks to not do cloning and formatting, because most of ticks are discarded in the code afterwards.
I think we can improve the performance here by simply cloning once before the loop starts, then continuously add to that value. We'd have to clone again when adding to me.ticks but that would be less overall clones since we aren't going to have a label for every second in 2 hours.
Is is possible to create a JSFiddle for your case that we can use as a benchmark?
@simonbrunel thoughts on this? @tannerlinsley you were heavily involved in the initial time scale stuff. Have any ideas/thoughts?
I made one optimization with time label format, but with possible memory leak (which does not occur in my project) in tickFormatFunction in scale.time.js if(this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat]!==undefined){ var formattedTick = this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat]; } else { var formattedTick = tick.format(this.displayFormat); this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat] = formattedTick; }
(I didn't made an pull request because it is not optimal too)
It helps a lot, because ticks are formatted just once and formatting itself is very slow. But the main problem still occurs which is cycle itself, because it is called with every redraw for much more ticks than needed.
I will try to edit it by myself by forcing me.scaleSizeInUnits to graph width/height and multiply i by ratio between original value and scaled value.
P.S.: My colleague made this fiddle https://jsfiddle.net/tomasz_svk/h2Lb0gjg/ and I found that cloning moment.js object is not that expensive as I thought (event with Array prototyping) for 500k instances.
Activity
etimberg commentedon Aug 24, 2016
I think we can improve the performance here by simply cloning once before the loop starts, then continuously add to that value. We'd have to clone again when adding to
me.ticksbut that would be less overall clones since we aren't going to have a label for every second in 2 hours.Is is possible to create a JSFiddle for your case that we can use as a benchmark?
@simonbrunel thoughts on this? @tannerlinsley you were heavily involved in the initial time scale stuff. Have any ideas/thoughts?
tannerlinsley commentedon Aug 24, 2016
Yeah that's a good point. The question is, are we using the later discarded moment objects for anything in the process beforehand?
bielitom commentedon Aug 25, 2016
I made one optimization with time label format, but with possible memory leak (which does not occur in my project) in
tickFormatFunctioninscale.time.jsif(this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat]!==undefined){ var formattedTick = this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat]; } else { var formattedTick = tick.format(this.displayFormat); this.tickFormatCache[tick.valueOf() + "_" + this.displayFormat] = formattedTick; }(I didn't made an pull request because it is not optimal too)
It helps a lot, because ticks are formatted just once and formatting itself is very slow. But the main problem still occurs which is cycle itself, because it is called with every redraw for much more ticks than needed.
I will try to edit it by myself by forcing
me.scaleSizeInUnitsto graph width/height and multiplyiby ratio between original value and scaled value.P.S.: My colleague made this fiddle https://jsfiddle.net/tomasz_svk/h2Lb0gjg/ and I found that cloning moment.js object is not that expensive as I thought (event with Array prototyping) for 500k instances.