jquery/jquery — pull request #1089 by hasclass (2012-12-18 16:02:25+00:00)
Title: Optimized $.type for primitives

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](http://jsperf.com/jquery-type-with-primitives)

[Benchmark for $.type with object](http://jsperf.com/jquery-type-for-objects)

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.

```javascript
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.