-
Notifications
You must be signed in to change notification settings - Fork 20.6k
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
Conversation
I like this, but please file a ticket http://bugs.jquery.com/—thanks!! |
sorry. will do. |
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? |
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. |
I accidentally pushed another performance improvement for 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. |
Hi @hasclass can you sign our CLA at http://jquery.github.com/cla.html ? Thanks! I'll break these apart into two commits. |
@dmethvin Thanks. Signed. Just fixed a potential bug with $.type.
return typeof obj === "object" || typeof obj === 'function' ?
class2type[ core_toString.call(obj) ] || "object" :
typeof obj; This doesn't effect the $("#foo") optimization. |
👏 👏 👏 👏 👏 👏 👏 |
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 |
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. |
Thanks @hasclass! 🌟 For the future, please put each pull request in its own branch. You'll need to force-reset your |
Also fixes browsers where `typeof RegExp === "function"`.
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.
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.