Optimize _.isObject. [ninjainvisible]

Former-commit-id: aa139d3eac1c9913ba6dc85c63145a121ab40cba
This commit is contained in:
John-David Dalton
2012-05-12 01:11:25 -04:00
parent ff93f7cbb5
commit 6a06bf5efa
2 changed files with 24 additions and 9 deletions

View File

@@ -35,6 +35,7 @@
'isFunc', 'isFunc',
'length', 'length',
'object', 'object',
'objectTypes',
'noaccum', 'noaccum',
'prop', 'prop',
'property', 'property',

View File

@@ -12,7 +12,9 @@
var argsLimit = Math.pow(2, 32) - 1; var argsLimit = Math.pow(2, 32) - 1;
try { try {
(function() { argsLimit = arguments.length; }).apply(null, Array(argsLimit)); (function() {
argsLimit = arguments.length;
}).apply(null, Array(argsLimit));
} catch(e) { } } catch(e) { }
/** Used to escape characters in templates */ /** Used to escape characters in templates */
@@ -40,6 +42,19 @@
/** Used to generate unique IDs */ /** Used to generate unique IDs */
var idCounter = 0; var idCounter = 0;
/**
* Used to check whether a value is the ECMAScript language type of Object.
* http://es5.github.com/#x8
*/
var objectTypes = {
'boolean': false,
'function': true,
'object': true,
'number': false,
'string': false,
'undefined': false
};
/** Used to restore the original `_` reference in `noConflict` */ /** Used to restore the original `_` reference in `noConflict` */
var oldDash = window._; var oldDash = window._;
@@ -419,13 +434,13 @@
// create the function factory // create the function factory
var factory = Function( var factory = Function(
'arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf, ' + 'arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf, ' +
'Infinity, isArray, isEmpty, slice, stringClass, toString, undefined', 'Infinity, isArray, isEmpty, objectTypes, slice, stringClass, toString, undefined',
'"use strict"; return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}' '"use strict"; return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}'
); );
// return the compiled function // return the compiled function
return factory( return factory(
arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf, arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf,
Infinity, isArray, isEmpty, slice, stringClass, toString Infinity, isArray, isEmpty, objectTypes, slice, stringClass, toString
); );
} }
@@ -2068,10 +2083,9 @@
* // => { 'name': 'moe' }; * // => { 'name': 'moe' };
*/ */
function clone(value) { function clone(value) {
if (value !== Object(value)) { return objectTypes[typeof value] && value !== null
return value; ? (isArray(value) ? value.slice() : extend({}, value))
} : value;
return isArray(value) ? value.slice() : extend({}, value);
} }
/** /**
@@ -2444,7 +2458,7 @@
* // => false * // => false
*/ */
function isObject(value) { function isObject(value) {
return value === Object(value); return objectTypes[typeof value] && value !== null;
} }
/** /**
@@ -2580,7 +2594,7 @@
*/ */
var keys = nativeKeys || createIterator({ var keys = nativeKeys || createIterator({
'args': 'object', 'args': 'object',
'exit': 'if (object !== Object(object)) throw TypeError()', 'exit': 'if (!objectTypes[typeof object] || object === null) throw TypeError()',
'init': '[]', 'init': '[]',
'inLoop': 'result.push(index)' 'inLoop': 'result.push(index)'
}); });