Bug Tracker

Modify

Ticket #13076 (closed feature: fixed)

Opened 16 months ago

Last modified 16 months ago

Performance optimization (10-30%) for $("some-selector")

Reported by: hasclass Owned by:
Priority: low Milestone: 1.9
Component: core Version: git
Keywords: Cc:
Blocking: Blocked by:

Description

The $( ) function currently has a bottleneck when querying with a string (primitive).

I optimized the method and it gets a performance boost of 0-30% depending on browser:

 http://jsperf.com/jquery-init-optimized

This is the current implementation of $( ). The first if clause checks for a nodeType property. When passing a String primitive this property check causes that string to be converted into a String wrapper object. Which slows everything down.

init: function( selector, context, rootjQuery ) {
  // ...
	// HANDLE: $(DOMElement) check first that selector is not a primitive
	if ( selector.nodeType ) { // ..
	}
	// Handle HTML strings
	if ( typeof selector === "string" ) {
	

The solution is to check first if selector is not a primitive (using typeof === 'object'). This has the effect that selector is not coerced to a string wrapper object. Typeof is fast.

init: function( selector, context, rootjQuery ) {
  // ...
	// HANDLE: $(DOMElement) check first that selector is not a primitive
	if ( typeof selector === "object" && selector.nodeType ) { // ..
	}
	// Handle HTML strings
	if ( typeof selector === "string" ) {
	

I'll create a pull request shortly.

Change History

comment:1 Changed 16 months ago by Sebi Burkhard

  • Status changed from new to closed
  • Resolution set to fixed

Fix #13076. Speed up $() with strings, part of gh-1089.

Changeset: d8298046311e66a47424ec3f3cbe77e97bee3958

comment:2 Changed 16 months ago by dmethvin

  • Priority changed from undecided to low
  • Type changed from bug to feature
  • Component changed from unfiled to core
  • Milestone changed from None to 1.9

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.