angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

orderBy filter performance regression

Open johnjcorcoran opened this issue 12 years ago • 19 comments

Hello,

We have noticed a performance regression for the orderBy filter when upgrading from Angular v1.2.x to v1.3. This only appears to happen in Chrome; Firefox seems fine.

Sorting arrays of around 6000 items appears to be rendering 5-6x slower than before, making some pages in our app appear suddenly very sluggish.

Browser: Chrome v38 Operating systems: Ubuntu 14.04 and Windows Web Server 2008 R2

In the examples below, note that performance is only sluggish with a combination of Chrome, Angular v1.3 and the use of orderBy. There may be other influential variables that I've not taken into consideration.

Examples: Angular 1.3, orderBy filter on 6000 strings: http://plnkr.co/edit/Xovvjy628wYwu78CxXiO?p=preview Angular 1.3, filter without an orderBy on 6000 strings: http://plnkr.co/edit/pSZTLGQS0IlprIxYMljU?p=preview Angular 1.2.25, orderBy filter on 6000 strings: http://plnkr.co/edit/s97OMk4iuiv2HsPpU01K?p=preview

Apologies for the unscientific nature of these examples, but I do think that they illustrate the issue.

johnjcorcoran avatar Nov 06 '14 12:11 johnjcorcoran

yeah, I can reproduce this... I'll upload the timelines I recorded, it looks like we're spending a ton more time in the orderBy compare() fns.

caitp avatar Nov 06 '14 21:11 caitp

v1.2.25 and v1.3.0 --- looks like tons of 10-20ms calls to compare(), + garbage being created and disposed of in the midst of sorting... hm.

caitp avatar Nov 06 '14 21:11 caitp

but eh, the 1.2.25 demo isn't actually doing any sorting, so maybe that has something to do with why it's so much faster? o_o

caitp avatar Nov 06 '14 21:11 caitp

you know what, I think this was just a case of the api being used incorrectly... it seems to be pretty fast in 1.3 when used properly, just gonna compare again to see...

caitp avatar Nov 06 '14 21:11 caitp

http://plnkr.co/edit/7IMsKa2zFj4HeYWiz1TT?p=preview this version is calling the orderBy API correctly, and it's getting through everything (the whole bootstrap + sorting + everything) in under 400ms.

http://plnkr.co/edit/2PE8nlz9WJ4CNB6VrFck?p=preview is the correct v1.2.25 version, and gets through in about 500ms (in this case), so the 1.3.0 version seems to be doing a bit better, although I wouldn't put much into that.

What I'm interested in though, is why it performs so badly in the case where the api is not being used correctly. That's definitely a regression.

caitp avatar Nov 06 '14 22:11 caitp

You get the same results if you just replace the repeat with ng-repeat="item in items | orderBy: undefined". it looks like at 1.3 the following short-circuit was removed if (!sortPredicate) return array;

lgalfaso avatar Nov 06 '14 23:11 lgalfaso

i wonder if we removed that on purpose

caitp avatar Nov 06 '14 23:11 caitp

i wonder if we removed that on purpose

I am tempted to say yes, the reason being that if the item is comparable then ng-repeat="item in items | orderBy: undefined" will order while this was not the case with 1.2

lgalfaso avatar Nov 06 '14 23:11 lgalfaso

the issue being that if you change the example to https://github.com/angular/angular.js/issues/9940#issuecomment-62068384, then it is fast once agaiin

lgalfaso avatar Nov 06 '14 23:11 lgalfaso

hmm, yeah that makes sense that we'd want to sort anyways, but I don't totally get why we sort so slow in this case. It's lightning fast when the predicate is properly specified. It's probably because [object Object] < [object Object] is going to return false always. Depending on the sort algorithm, that could do bad things for runtime, but I am not sure how we're sorting in Array#sort

caitp avatar Nov 06 '14 23:11 caitp

@caitp the easy answer is that the sort is not very efficient for these cases

$ node
> i = 0; Array.apply(null, { length: 5000 }).map(Number.call, Number).sort(function(a, b) {++i; return b-a;});i
76418
> i = 0; Array.apply(null, { length: 5000 }).map(Number.call, Number).sort(function(a, b) {++i; return a-b;});i
79065
> i = 0; Array.apply(null, { length: 5000 }).map(Number.call, Number).sort(function() {++i; return 1;});i
6410710
> i = 0; Array.apply(null, { length: 5000 }).map(Number.call, Number).sort(function() {++i; return -1;});i
6360930

lgalfaso avatar Nov 08 '14 19:11 lgalfaso

so there you go, the problem is that there's no real answer to the question "which is smaller", because it's true for both a < b and b < a, so the loop doesn't handle this.

caitp avatar Nov 08 '14 20:11 caitp

lets close this as a non-bug then, because I think we'd rather use sort() than rolling our own sort algorithm

caitp avatar Nov 08 '14 20:11 caitp

the one tweak I can think of is the following:

  • map each property that is being compared from each element in the original array to a new array
  • order the array of the properties
  • use the ordered array of the properties to reorder the original array

The main difference is that in the previous case we would call each $parse expression O(n*logN) [or in this case it look like O(n^2) times] while in the other we would only call each $parse expression n times

lgalfaso avatar Nov 08 '14 20:11 lgalfaso

I don't think that's going to work, unfortunately. Mapping zillions of properties (without something like $$hashKey) is gonna be way worse than it already is. Using $$hashKey would be less than ideal, too. And it's a bug in the users code anyways

caitp avatar Nov 08 '14 20:11 caitp

I was thinking something like

originalArray.map(function(x) { return {prop0: get0(x), prop1: get1(x), /* other properties*/, value: x};).sort(function(a,b) { return compare(a.prop0, b.prop0) || compare(a.prop1, b.prop1) || ... || 0;}).map(function(x) { return x.value;});

it has the side-effect that it generates a lot of garbage, so it is not for free

lgalfaso avatar Nov 08 '14 20:11 lgalfaso

it has the side-effect that it generates a lot of garbage

This is why we can't do it

caitp avatar Nov 08 '14 20:11 caitp

The docs for orderBy give an example of a table that is sorted according to a scope variable. When a table heading is clicked, the sort variable is adjusted.

The example initialises the sort variable to one of the column headings, but I think this issue might affect tables that do not start off sorted i.e the sort variable is not initialised or is initialised to an empty string.

Is there a way to either:

a) orderBy : nothing b) only apply the orderBy filter once the sort variable has been set?

Thanks

danhaller avatar Nov 12 '14 12:11 danhaller

@danhaller - I am having similar requirement. Please let me know if you happened to get any feasible solution to ordeby: field if defined else drop the orderby clause.

vinuthamsr avatar Mar 21 '15 15:03 vinuthamsr