How to increase performance of this js script (function calls exponentially increasing)?
<p>Any general tips how to increase a longish js code like this? It works great but sometimes it gets a little sluggish and all the drag/drop and ajax features slow down.</p>

<p>I know this code is a little longer so I am not looking for specific suggestions. Just your first thoughts after taking a quick glance at this code.</p>

<p>EDIT:</p>

<p>I have found a pretty scary thing. The calls to dragDrop() are exponentially increasing in the $('.folderListOnclick').click event. After each click dragDrop() is called 1 time, 2 times, 4 times, 8 times. That's what's slowing it down.</p>

<p>But I don't understand why it is happening.</p>

<pre><code>&lt;script type="text/javascript"&gt;
    //&lt;!--
$(document).ready(function() {

    function in_array (needle, haystack, argStrict) {
        var key = '', strict = !!argStrict; 
        if (strict) {
            for (key in haystack) {
                if (haystack[key] === needle) {
                    return true;            }
            }
        } else {
            for (key in haystack) {
                if (haystack[key] == needle) {
                    return true;
                }
            }
        }
         return false;
    }

    var openedFolders = new Array();

    var start = 0;
    var stop = 0;
    $('.drag').each(function() {
        var draggables = $(this).parents('table').find('.drag');
        var $next = draggables.filter(':gt(' + draggables.index(this) + ')').first();
        var width = $(this).css('width');
        var nextWidth = $next.css('width');
        if (nextWidth &gt; width /*&amp;&amp; 30 == parseInt(width)*/) {
            $(this).removeClass('ordinaryFolderClosed');
            $(this).removeClass('ordinaryFolderOpened');
            if (in_array($(this).attr('rel'), openedFolders)) {
                $(this).addClass('ordinaryFolderOpened');
            } else {
                $(this).addClass('ordinaryFolderClosed');
            }               
        }
        if (in_array($(this).attr('rel'), openedFolders)) {
            start = 1;
        }
        if (1 == start &amp;&amp; stop &lt; 2) {               
            if (30 == parseInt(width)) {
                stop++;
            }
        } else {
            start = 0;
            stop = 0;
            if (parseInt(width) &gt; 30) {
                $(this).parent().parent().hide();
            }
        }
    });

    function dragDrop()
    {       
        $('.folders .trow1').hover(
            function () {
                if ($(this).css('backgroundColor') != 'rgb(52, 108, 182)' &amp;&amp; $(this).css('backgroundColor') != '#346cb6') {
                    $(this).css('background', "#C2E3EF");
                }
            },
            function () {
                if ($(this).css('backgroundColor') != 'rgb(52, 108, 182)' &amp;&amp; $(this).css('backgroundColor') != '#346cb6') {
                    $(this).css('background', 'transparent');   
                }
            }
        );

        $('.drag').click(function() {

            if ($(this).hasClass('noclick')) {
                $(this).removeClass('noclick');
            } else {

                var draggables = $(this).parents('table').find('.drag');
                var $next = draggables.filter(':gt(' + draggables.index(this) + ')').first();
                var width = $(this).css('width');
                var nextWidth = $next.css('width');
                if (nextWidth &gt; width /*&amp;&amp; 30 == parseInt(width)*/) {
                    var isVisible = $next.is(':visible');
                    if (isVisible) {
                        $(this).removeClass('ordinaryFolderClosed');
                        $(this).removeClass('ordinaryFolderOpened');
                        $(this).addClass('ordinaryFolderClosed');
                    } else {
                        $(this).removeClass('ordinaryFolderClosed');
                        $(this).removeClass('ordinaryFolderOpened');
                        $(this).addClass('ordinaryFolderOpened');
                    }
                    clickedId = $(this).attr('rel');
                    clickedId = clickedId.split(',');
                    clickedType = clickedId[1];
                    clickedId = clickedId[0];
//                  $.ajax({
//                      type:    'POST',
//                      url:     'body/obsah/mediaManager/setOpenedFolder.php',
//                      data:    'id='+clickedId+'&amp;type='+clickedType,
//                      success: function(msg){
//                          //alert(msg);
//                      }
//                  });
                    var start = 0;
                    var stop = 0;
                    var i = 0;
                    // close folder
                    if (isVisible) {
                        $('.drag').each(function() {
                            if (0 == start) {
                                iteratedId = $(this).attr('rel');
                                iteratedId = iteratedId.split(',');
                                iteratedId = iteratedId[0];
                                if (iteratedId == clickedId) {
                                    start = 1;
                                }
                            }
                            if (1 == start &amp;&amp; stop &lt; 2) {
                                if ($(this).css('width') &gt; width) {
                                    $(this).parent().parent().hide();
                                    if ($(this).hasClass('ordinaryFolderClosed') || $(this).hasClass('ordinaryFolderOpened')) {
                                        $(this).removeClass('ordinaryFolderClosed');
                                        $(this).removeClass('ordinaryFolderOpened');
                                        $(this).addClass('ordinaryFolderClosed');
                                    }
                                } else {
                                    stop++;
                                }
                            }
                            i++;
                        }); 
                    }
                    // open folder
                    else {
                        $('.drag').each(function() {
                            if (0 == start) {
                                iteratedId = $(this).attr('rel');
                                iteratedId = iteratedId.split(',');
                                iteratedId = iteratedId[0];
                                if (iteratedId == clickedId) {
                                    start = 1;
                                }
                            }
                            if (1 == start &amp;&amp; stop &lt; 2) {
                                if (parseInt($(this).css('width')) == parseInt(width)+5) {
                                    $(this).parent().parent().show();                   
                                }

                                if (parseInt($(this).css('width')) == parseInt(width)) {
                                    stop++;
                                }
                            }
                            i++;
                        });
                    }
                }

            }
        });

        var dragId = 0;
        var dragType = 0;
        var dropId = 0;
        var dropType = 0;
        var isFile = false; 

        $('.drag').draggable({ 
            revert: true,
            cursorAt: {top: 0, left: 0},
            drag: function() {
                if ($(this).attr('rel') !== undefined) {
                    dragId = $(this).attr('rel');
                    dragId = dragId.split(',');
                    dragType = dragId[1];
                    dragId = dragId[0];
                }
                isFile = false;
            },
            start: function(event, ui) {
                 $(this).addClass('noclick');
            }
        });

        $('.drag2').draggable({
            revert: true,
            cursorAt: {top: 0, left: 0},
            drag: function() {
                if ($(this).attr('rel') !== undefined) {
                    dragId = $(this).attr('rel');
                    dragId = dragId.split(',');
                    dragType = dragId[1];
                    dragId = dragId[0];
                }
                isFile = true;
            }
        });     

        $('.drop').droppable({
            tolerance: 'pointer',
            drop: function() {
                if ($(this).attr('rel') !== undefined) {
                    dropId = $(this).attr('rel');
                    dropId = dropId.split(',');
                    dropType = dropId[1];
                    dropId = dropId[0];
                    if (dropId != dragId) {
                        if (false == isFile) {
                            $.ajax({
                                type:    'POST',
                                url:     'body/obsah/mediaManager/folder_move.php',
                                data:    'nid='+dragId+'&amp;pid='+dropId+'&amp;ft='+dropType,
                                success: function(msg){
                                    ajaxElementCall('left1', 'body/obsah/mediaManager/folder_list.php?type=1', 'right1', 'body/obsah/mediaManager/file_list.php?type=1&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0&amp;idFolder=0');
                                    dragDrop();
                                }
                            });
                        } else if (true == isFile) {
                            $.ajax({
                                type:    'POST',
                                url:     'body/obsah/mediaManager/file_move.php',
                                data:    'fid='+dragId+'&amp;did='+dropId+'&amp;ft='+dropType,
                                success: function(msg){
                                    ajaxElementCall('right1', 'body/obsah/mediaManager/file_list.php?type=1&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0&amp;idFolder=0&amp;reloadTree=0');
                                    dragDrop();
                                }
                            });
                        }
                    }
                }           
            }
        });

    }

    dragDrop();

    $('.folderListOnclick').click(function() {
        var append = 'idFolder='+$(this).attr('rel')+'&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0&amp;reloadTree=0';
        ajaxElementCall('right1', 'body/obsah/mediaManager/file_list.php?type=1&amp;'+append);
        dragDrop();
        $('.trow1').css('background', 'transparent');
        $('.trow1').css('color', '#3e4245');
        $(this).parent().css('background', "#346cb6 url('img/menuButtonOver.png') left top repeat-x");
        $(this).parent().css('color', 'white');
    });

    $('.folderEditOnclick').click(function() {
        var append = 'idFolder='+$(this).attr('rel')+'&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0';
        showModal('modal_div', 'Editácia adresára'); 
        ajaxElementCall('modal_div', 'body/obsah/mediaManager/folder_edit.php?kam=edit1&amp;'+append);
    });

    $('.folderDeleteOnclick').click(function() {
        var append = 'idFolder='+$(this).attr('rel')+'&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0';
        showModal('modal_div', 'Vymazanie adresára'); 
        ajaxElementCall('modal_div', 'body/obsah/mediaManager/folder_delete.php?kam=del1&amp;'+append);
    });

    $('.addNewFolder').click(function() {
        showModal('modal_div', 'Nový adresár');
        var id = '0';
        $('.folders .trow1').each(function() {
            if ($(this).css('backgroundColor') == 'rgb(52, 108, 182)' || $(this).css('backgroundColor') == '#346cb6') {
                id = $(this).attr('rel');
                id = id.split(',');
                id = id[0];
            }
        });
        ajaxElementCall('modal_div', '/body/obsah/mediaManager/folder_add.php?type=1&amp;kam=new1&amp;idFolder='+id+'&amp;browse=0&amp;assignType=0&amp;CKEditorFuncNum=0');
    });

});    //--&gt;

&lt;/script&gt;
</code></pre>
