JQuery UI Sortable performance while sorting

jqueryjquery-uijquery-ui-sortable

I have a calendar being used in production with 60 days and up to 5 sortable items per day, so that's 300 sortable items. The days are <td> elements and the events are <div>s.

Both sorting within a day and dragging from one day to another are slow. The element freezes up for a bit when it enters a new day or when it passes over another sortable item.

The delay seems related to both the number of days and the number of sortable items.

Here is the jQuery code.

$('.calendar-schedule-day').sortable({
    items: ".service-trip, .calendar-event",
    connectWith: ".calendar-schedule-day"
});

How can I reduce the delay that occurs when sorting?

More info

Where chrome is calling Layout and RecalculateStyle many times in a row, there is the following warning:

Layout – Details
Duration 15.000 ms (at 36.86 s)
Note Forced synchronous layout is a possible performance bottleneck.
Layout invalidated
….


Update: jsFiddle here – it is so unusably slow that I can't tell if it reproduces the problem or not. It is not like that in production. But if I start removing html (like weeks) from the example then I may not be able to reproduce the problem anyway.

Best Answer

Seems i got it a lot faster by hiding all sortable elements before calling sortable and after it i show them again.

$('.calendar-schedule-day').hide();
$('.calendar-schedule-day').sortable({ ... });
$('.calendar-schedule-day').sortable({ connectWith: ... });
$('.calendar-schedule-day').show();

JSFiddle

Better way to fix it

just use this selector $('.calendar-schedule-day:visible') instead of $('.calendar-schedule-day') when using .sortable()

JSFiddle

Seems that the hide elements messes with sortable, so we will only make the visible elements sortable.