Skip to content

Optimized $.type and $( str ) #1089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

Conversation

hasclass
Copy link
Contributor

Optimized $.type( obj ) when obj is a primitive. This speeds up that method by a magnitude without sacrificing performance when obj is not a primitive.

Benchmark for $.type with primitives

Benchmark for $.type with object

The trick is to check first using typeof (which is extremely fast) whether an object is a primitive or not. obj is a primitive if it isnt typeof 'object'. For primitives avoid the costly core_toString call which converts a primitive into a wrapper object.

type: function (obj) {
  if ( obj == null ) {
    return false;
  }
  return typeof obj === 'object' ?
    // handles {}, [], new String, new Number, etc.:
    class2type[ core_toString.call(obj) ] || "object"  : 
    // handles 1, 'foo', function () {}, true:
    typeof obj;
  }
}

Passing a wrapper object like new String("foo") will use the normal core_toString.

This technique has a significant performance impact on old and (especially) new browsers.

Additionally I added 4 tests for $.type to also test wrapper objects.

@rwaldron
Copy link
Member

I like this, but please file a ticket http://bugs.jquery.com/—thanks!!

@hasclass
Copy link
Contributor Author

sorry. will do.

@dmethvin
Copy link
Member

It's a small size-change so it seems worth landing, but I doubt there is any place where this removes a bottleneck in code. The original code isn't that slow in clock terms. Do you have profiler runs where this is a hot spot?

@hasclass
Copy link
Contributor Author

@hasclass
Copy link
Contributor Author

Fair enough. I haven't run any profiler or found a hot spot. In fact type( ) checks with a primitive isn't checked very often. I was just being idealistic.

@hasclass
Copy link
Contributor Author

I accidentally pushed another performance improvement for $( ) with string selectors, e.g. $("#foo").

hasclass@e5cc091

This makes $("#foo") upto 0-30% faster depending on browser. http://jsperf.com/jquery-init-optimized

http://bugs.jquery.com/ticket/13076

Sorry for the mess.

@dmethvin
Copy link
Member

Hi @hasclass can you sign our CLA at http://jquery.github.com/cla.html ? Thanks! I'll break these apart into two commits.

@hasclass
Copy link
Contributor Author

@dmethvin Thanks. Signed.

Just fixed a potential bug with $.type.

typeof /foo/ in some engines returns function. So that would cause a bug. The latest commit fixes it. function/regexps are also evaluated using core_toString:

return typeof obj === "object" || typeof obj === 'function' ?
    class2type[ core_toString.call(obj) ] || "object" :
    typeof obj;

This doesn't effect the $("#foo") optimization.

@mikesherov
Copy link
Member

👏 👏 👏 👏 👏 👏 👏

@dmethvin
Copy link
Member

Ouch, at this point the #13075 fix is +26 gzip, which is very large for something that isn't a bottleneck. In what browsers does typeof RegExp === "function" today? I know that used to be true in old Webkit but it doesn't seem true in Safari 5.1 or current Chrome.

@dmethvin
Copy link
Member

Correction, it's +10 gzip ... the compare_size task was gzipping the map file and I didn't notice. Let me see if this can be compressed a bit more.

@dmethvin
Copy link
Member

Thanks @hasclass! 🌟

For the future, please put each pull request in its own branch. You'll need to force-reset your master now because it won't match the one here. It takes a while to get the hang of making clean pull requests, but we'd love it if you continued to contribute.

mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
Also fixes browsers where `typeof RegExp === "function"`.
mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants