primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Performance

Open izll opened this issue 10 years ago • 28 comments

Hi,

When I quickly moving the mouse over some components (especially over datatable), the cpu usage grows up highly (it also occurs in firefox and chrome) (perhaps best seen in here: http://www.primefaces.org/primeng/#/datatableselection ). It also happens when I using components in datatable, like input or autocomplete and clicking into them. It makes my app laggy because I use datatable in many places. I think it's angular problem, but it's occours everyone?

izll avatar Aug 07 '16 16:08 izll

That's not angular really, this is a byproduct of the datatable itself. There are two methods defined.

mouseenter and mouseleave, and these are being called when your mouse cursor enters any of the rows or columns. This isn't new, this used to bog down JTable in Swing.

The problem you appear to have is mouseenter in the datatable and then the mouseenter or hover functions being called for every component in the row.

I don't think the NGPrime team could actually optimise for that.

garethlewis avatar Aug 08 '16 12:08 garethlewis

Thanks, I think it's a big problem. My temporary workaround is to catch mousemove, mouseout, mouseover, mouseenter, mouseleave, scroll events, and if the event target or related target not matching that what I need then I call event preventDefault and stopPropagation methods.

izll avatar Aug 08 '16 12:08 izll

Would love to see a fix for that. Indeed a very big problem. For now I'll try to implement @izll 's workaround. @izll , could you spare an example of how and where you catch those events?

jonyadamit avatar Aug 29 '16 07:08 jonyadamit

Yes, of course, just include this in your app.html after jquery:

document.addEventListener("mousemove", function (event) {
    var target = $(event.target);
    if (target.is("path")) {
        return;
    }
    event.preventDefault();
    event.stopPropagation();
    event.cancelBubble = true;
}, true);

document.addEventListener("mouseout", function (event) {
    var target = $(event.target);
    if (target.is("rect")) {
        return;
    }
    event.preventDefault();
    event.stopPropagation();
    event.cancelBubble = true;
}, true);

document.addEventListener("scroll", function (event) {
    event.preventDefault();
    event.stopPropagation();
    event.cancelBubble = true;
}, true);


document.addEventListener("mouseover", function (event) {
    var target = $(event.target);
    if (target.is("a") || target.is("img") || target.is("rect")) {
        return;
    }
    event.preventDefault();
    event.stopPropagation();
}, true);

document.addEventListener("mouseenter", function (event) {
    var target = $(event.target);
    if (target.hasClass('ui-autocomplete-list-item') || target.hasClass('ui-dropdown-item')) {
        return;
    }
    var relatedTarget = $(event.relatedTarget);
    if (relatedTarget.hasClass('ui-autocomplete-list-item') || relatedTarget.hasClass('ui-dropdown-item')) {
        return;
    }
    event.preventDefault();
    event.stopPropagation();
}, true);

document.addEventListener("mouseleave", function (event) {
    var target = $(event.target);
    if (target.is("a") || target.is("img") || target.is("rect")) {
        return;
    }
    if (target.hasClass('ui-autocomplete-list-item') || target.hasClass('ui-dropdown-item')) {
        return;
    }
    var relatedTarget = $(event.relatedTarget);
    if (relatedTarget.hasClass('ui-autocomplete-list-item') || relatedTarget.hasClass('ui-dropdown-item')) {
        return;
    }
    event.preventDefault();
    event.stopPropagation();
}, true);

izll avatar Aug 29 '16 12:08 izll

Thanks @izll ! Of course this workaround is only good as a temporary workaround indeed. You could also use HostListener to catch these events "the angular way" and use it only on the relevant component.

Digging around the code I noticed the datatable handles change detection by it's own.. calling this complex change detection logic for every mouse move seems insane to me. Changing this method to perform reference comparison is my temporary fix - I still have great lag so I have to dig more, but it's much better than before.

jonyadamit avatar Aug 30 '16 11:08 jonyadamit

Hi guys,

Is there any plan to address this issue? Not sure why it's not upvoted more since it makes the DataTable pretty much unusable in my experience... Thanks!

tbertran avatar Sep 07 '16 18:09 tbertran

Switched to another table for now. This one's a showstopper..

jonyadamit avatar Sep 08 '16 08:09 jonyadamit

@jonyadamit I have same issue with datatable, which one you choose to solve this?

sentyaev avatar Sep 27 '16 17:09 sentyaev

@sentyaev I don't think it would be right of me to endorse specific alternatives in here, I was mainly conveying the seriousness of this issue. I will nevertheless give you that answer in private 😉

jonyadamit avatar Sep 27 '16 20:09 jonyadamit

I think, the problem is more general in primeng. It's about setting the ui-state-* classes for stylings. Every mouse enter and leave event kicks in a change detection phase. This can cause big cpu usage on a comppex page. I think this is a bad legacy from the jquery based css of primefaces, where supporting older browsers was a goal. But since angular is targeted for modern browsers, I don't see any reason to continue this bad pattern. CSS can be used for this definitely.

This issue needs to be targeted, otherwise there will be always performance problems in the framework.

ribizli avatar Oct 18 '16 04:10 ribizli

How are you going to enable and disable the css for the row you need? CSS might speed it up, but you still need to decide which column or row the mouse has entered.

I have never noticed an issue with Chrome. I have with an older version of Edge, but since the latest version of 1607, not again. Firefox on the other hand is treacle.

garethlewis avatar Oct 18 '16 09:10 garethlewis

@garethlewis using :hover?

ribizli avatar Oct 18 '16 09:10 ribizli

Not going to help, actually that is worse. hover causes a reflow event in the brower, where the browser as to work out what has changed. That's even worse than you setting the css yourself.

garethlewis avatar Oct 18 '16 09:10 garethlewis

What can be worse than triggering Angular Change Detection on every mouse enter / leave. This happens everywhere: inputs, buttons, all components. And I don't believe that changing a class don't do a reflow as well.

ribizli avatar Oct 18 '16 10:10 ribizli

True, but just replacing everything with hover, doesn't solve anything. Doing anything with CSS, like changing an attribute causes a reflow.

garethlewis avatar Oct 18 '16 10:10 garethlewis

The solution we followed for this performance issue was to more-or-less implement a custom DataTable component ourselves, which uses a separate Angular 2 component for each row. The problem with the PrimeNG DataTable is that for many columns and rows each template expression, generated in the DataTable template (using the ngFor loops), will need to be checked in each change detection cycle. The next problem in solving this was, that the PrimeNG DataTable itself is unable to support Angular 2 OnPush change detection, because it is just one component. Therefore, we used a custom "row" component with OnPush. The next problem was that in Angular 2 each component's selector element, which is used to include that component in a parent's HTML/template, will always be rendered in the final HTML output and making a custom "row" element being a <tr> element is not possible in Angular 2. To solve this we used a custom <div> element with a "display: table-row;" style to make it layout just like a table row <tr> element.

httpdigest avatar Nov 10 '16 14:11 httpdigest

@httpdigest Would you be willing to open source your solution as an alternate datatable implementation?

TekSiDoT avatar Nov 17 '16 09:11 TekSiDoT

I just sketched a possible solution for the performance problem, which we just implemented a simple scaffold for. This is in no way a complete solution that can readily replace the existing PrimeNG DataTable component in its functionality. My hopes were that PrimeTek would pick up on the solution strategy and realize that in their own DataTable. Or that they just comment this ticket for once. :)

httpdigest avatar Nov 17 '16 09:11 httpdigest

I'd be happy to take a look and possibly adapt it, you could post a pull-request to my fork.

TekSiDoT avatar Nov 17 '16 09:11 TekSiDoT

Yep, this is a very bad decision - i have 100% CPU usage on my laptop (intel core i7, ssd and 16gb ram)! CSS pseudo classes :hover and :active can help with this issue.

UPD.: also i see in timeline (chrome debug tools) when set in datatable 100 rows - change detector going crazy. Is this article helps with refactoring https://www.lucidchart.com/techblog/2016/05/04/angular-2-best-practices-change-detector-performance/ ?

brud avatar Dec 27 '16 18:12 brud