Best of jsperf (2000-2013) – Part 3/3

Sam Deering
Share

Part 3 of the jsperf best of. Enjoy!

Miscellaneous Speed Tests

jQuery Chaining

Tests whether chaining jQuery function calls is noticeably faster than separate calls. Chaining makes little difference to code performance
jquery-chaining

Ternary Operators

Check what is faster, reassigning the same value/object to a variable, or changing its value with an if. See JavaScript Shorthand Techniques to improve your shorthand knowledge.
ternary-vs-if

Global Variables vs Local Variables

Another reason not to use global variables.
GLobal-vs-local

Try/Catch Error Check Undefined

If vs. Switch

Comparing a if vs. a switch statement. switch is faster?
if-vs-switch

A closer look at jQuery $.empty()

jquery-empty

//Empty function from jQuery 1.9
    empty: function() {
        var elem,
            i = 0;

        for ( ; (elem = this[i]) != null; i++ ) {
            // Remove element nodes and prevent memory leaks
            if ( elem.nodeType === 1 ) {
                jQuery.cleanData( getAll( elem, false ) );
            }

            // Remove any remaining nodes
            while ( elem.firstChild ) {
                elem.removeChild( elem.firstChild );
            }

            // If this is a select, ensure that it displays empty (#12336)
            // Support: IE<9
            if ( elem.options && jQuery.nodeName( elem, "select" ) ) {
                elem.options.length = 0;
            }
        }

        return this;
    },

Other interesting jsperfs

  • The slowest thing ever vs the fastest thing ever http://jsperf.com/o
  • 2 ways of getting timestamp http://jsperf.com/currts
  • Good overview of the speed of the $.map() function http://jsperf.com/map/2
  • Good look at the different MD5 security encrption methods http://jsperf.com/md5-shootout
  • Number formatting 1,000,000 http://jsperf.com/number-format
  • Slice vs substring vs substr – pretty much the same http://jsperf.com/slice-vs-substring-vs-substr