Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsTree sort function ridiculously slow

Tags:

jquery

jstree

I am writing a Chrome extension for Delicious bookmarks. My background.js file fetches the bookmarks on browser open and creates the object required for jsTree to build a proper tree.

If I disable jsTree's sort plugin, the bookmarks appear immediately when I click the popup. If I enable the sort function there is a ~2 second delay between click and displaying of data.

I tried pre-sorting all of my data in the background script and passing it to the popup pre-sorted, but jsTree does not honor this sorted data.

I only have ~90 tags and ~400 bookmarks. Is there a config option I can use to make this faster? Here is what my jsTree looks like.

$('#jstree').jstree({
    'close_all': -1,
    'core': {
        'animation': 0
    },
        'json_data': {
        'async': true,
        'data': data
    },

    'progressive_render': true,
    'themes': {
        'theme': 'classic',
        'dots': false,
        'icons': true
    },

    'sort': function (a, b) {
        return this.get_text(a) > this.get_text(b) ? 1 : -1; 
    },

    'types': {
        'valid_children': [ 'folder' ],
        'types': {
            'folder': {
                'valid_children': [ 'file' ],
                'max_depth': 1
            },
        }
    },

    'plugins': [
        'json_data',
        'themes',
        'sort',
        'types',
    ]
});
like image 596
gdanko Avatar asked Feb 19 '13 18:02

gdanko


1 Answers

jsTree uses the native array sort method, so the only thing that can be improved is the calls to get_text, but I doubt that can get much quicker.

If you have your data presorted - simply remove the sort plugin from your plugins config array. There is no way for the sort plugin to "know" that the data is already sorted, just remove the plugin.

like image 73
vakata Avatar answered Oct 15 '22 08:10

vakata