Skip to content

Instantly share code, notes, and snippets.

@gibson042
Last active August 29, 2015 14:14
Show Gist options
  • Save gibson042/958d433d6aa3f0fc642b to your computer and use it in GitHub Desktop.
Save gibson042/958d433d6aa3f0fc642b to your computer and use it in GitHub Desktop.
jQuery optimization ideas

jQuery optimization ideas

Size

Simplify pnum

[eE] is overkill while all regular expressions incorporating pnum are theirselves case-insensitive.

jQuery.cssNumber values

Values are currently true (e.g., jQuery.cssNumber.lineHeight === true). Replacing them with the still-truthy " " would allow for references like jQuery.cssNumber[ prop ] || "px" instead of jQuery.cssNumber[ prop ] ? "" : "px".

Risks

We may have encouraged people to extend jQuery.cssNumber theirselves, in which case the size benefit is probably too small to outweigh the breakage.

jQuery.ifFunction

Combining jQuery.isFunction with subsequent invocation was rejected in jquery/jquery#828, but partly because of a naming convention that didn't take off.

Stopping animations

.stop and .finish are very similar, and could be merged or include even more repeated strings.

Consistency

jQuery.each callback arguments

Unlike jQuery.map and jQuery.grep, jQuery.each gives callbacks the array index/property name first. There's no fixing the existing method, but there's also nothing stopping us from introducing a new one (e.g., jQuery.loop). Internal use (e.g., no more function( _, arg ) {) might even make the size change a net benefit.

Invalid input behavior

jquery/jquery#2134

Performance

Inline argument slicing

There may be a performance penalty for passing around arguments objects, even to Array.prototype.slice (1, 2, …). If so, we can slice manually with a loop.

Data refactor

  • Simplify the Data prototype
    • Remove unused discard method
    • Remove unused initial arguments
    • Fold register method into cache
    • Consider changing cache to return falsy instead of object when accepts fails
    • Merge array/string paths in remove (e.g., if ( !jQuery.isArray( key ) ) { key = key.match( rnotwhite ) })
    • delete instead of register for remove( owner ); remove writability from defineProperty call
    • Add a method for conditional set (e.g., dataPriv.get( owner, key, defaultValue ))
  • Simplify cloneCopyEvent:
    • Consolidate "user" and "private" data variables
    • Use access to infer hasData
    • Repeat the srcData = cache.access( src ); destData = cache.set( dest, srcData ) pattern for both private and user data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment