четверг, 10 января 2013 г.

Effective Mootools usage tips | Cоветы эффективного использования Mootools

  • Never use the search by selector, beginning with the class:
    $$('.class some other selectors')
    The search results that will select all elements in the document, and then some of them will be filtered. If you add a name tag at the beginning, then the document will be selected only those elements, the name tag of the specified. Performance gains can be tens of times for large documents! Sample code:
    console.time("fast");
    console.log($$('div.tabmenu-icon').length);
    console.timeEnd("fast");
    
    console.time("slow");
    console.log($$('.tabmenu-icon').length);
    console.timeEnd("slow");
    
    console.time("all");
    console.log($$('*').length); // count all elements on page
    console.timeEnd("all");
    Output:
    164
    fast: 5ms
    164
    slow: 13ms
    3991
    all: 9ms
  • Никогда не используйте поиск по селектору, начинающегося с класса:
    $$('.class some other selectors')
    Такой поиск приводит к тому, что будут выбраны все элементы документа, а далее часть из них будет отфильтрована. Если же вы добавите имя тэга в начале, то из документа будут выбраны только те элементы, имя тэга которых совпадает с указанным. Прирост производительности может достигать десятков раз на больших документах! Пример кода:
    console.time("fast");
    console.log($$('div.tabmenu-icon').length);
    console.timeEnd("fast");
    
    console.time("slow");
    console.log($$('.tabmenu-icon').length);
    console.timeEnd("slow");
    
    console.time("all");
    console.log($$('*').length); // Подсчет общего числа элементов на странице
    console.timeEnd("all");
    Вывод:
    164
    fast: 5ms
    164
    slow: 13ms
    3991
    all: 9ms

Комментариев нет:

Отправить комментарий

Spam, junk and other unused material can be removed without notification.
Спам, мусор и другие неиспользованные материалы могут быть удалены без предварительного уведомления.