Jump to content

Recommended Posts

Posted (edited)
(function goodExample($) {
    var $selector = $('.someClass');

    // Stop flickering
    $selector.hide();
    $selector.css({
        background: 'blue',
    });
    $selector.show();
}(window.jQuery));

(function badExample($) {
    // Stop flickering
    $('.someClass').hide();
    $('.someClass').css({
        background: 'blue',
    });
    $('.someClass').show();
}(window.jQuery));

By not caching jQuery selector objects, means each time the DOM needs to be traversed to find each element with the class name of 'someClass', which is an expensive operation.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 4 months later...
Posted

Updated the example to adhere to the Airbnb style guide

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...