Skip to content

Slow performance using moment.js #3208

Closed

Description

@bielitom

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.

Activity

  1. etimberg commented on Aug 24, 2016

    @etimberg
    Member

    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?

  2. tannerlinsley commented on Aug 24, 2016

    @tannerlinsley
    Contributor

    Yeah that's a good point. The question is, are we using the later discarded moment objects for anything in the process beforehand?

  3. bielitom commented on Aug 25, 2016

    @bielitom
    Author

    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.

  4. added this to the Version 2.5 milestone on Jan 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions