Optimize _.invert, _.pairs, and _.values.

Former-commit-id: d2725dc8c75254784d450f2a7e997e079b8c3183
This commit is contained in:
John-David Dalton
2012-12-18 02:45:56 -08:00
parent 12bc852c89
commit 7bea30b2e6
2 changed files with 49 additions and 35 deletions

View File

@@ -97,7 +97,7 @@
'indexOf': ['sortedIndex'], 'indexOf': ['sortedIndex'],
'initial': [], 'initial': [],
'intersection': ['indexOf'], 'intersection': ['indexOf'],
'invert': ['forOwn'], 'invert': ['keys'],
'invoke': ['forEach'], 'invoke': ['forEach'],
'isArguments': [], 'isArguments': [],
'isArray': [], 'isArray': [],
@@ -129,7 +129,7 @@
'object': [], 'object': [],
'omit': ['forIn', 'indexOf'], 'omit': ['forIn', 'indexOf'],
'once': [], 'once': [],
'pairs': ['forOwn'], 'pairs': ['keys'],
'partial': ['isFunction', 'isObject'], 'partial': ['isFunction', 'isObject'],
'pick': ['forIn'], 'pick': ['forIn'],
'pluck': ['map'], 'pluck': ['map'],
@@ -155,7 +155,7 @@
'uniq': ['identity', 'indexOf'], 'uniq': ['identity', 'indexOf'],
'uniqueId': [], 'uniqueId': [],
'value': ['mixin'], 'value': ['mixin'],
'values': ['forOwn'], 'values': ['keys'],
'where': ['filter', 'keys'], 'where': ['filter', 'keys'],
'without': ['indexOf'], 'without': ['indexOf'],
'wrap': [], 'wrap': [],

View File

@@ -892,6 +892,26 @@
*/ */
var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions); var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions);
/**
* Creates an array composed of the own enumerable property names of `object`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns a new array of property names.
* @example
*
* _.keys({ 'one': 1, 'two': 2, 'three': 3 });
* // => ['one', 'two', 'three'] (order is not guaranteed)
*/
var keys = !nativeKeys ? shimKeys : function(object) {
// avoid iterating over the `prototype` property
return typeof object == 'function' && propertyIsEnumerable.call(object, 'prototype')
? shimKeys(object)
: (isObject(object) ? nativeKeys(object) : []);
};
/** /**
* A fallback implementation of `isPlainObject` that checks if a given `value` * A fallback implementation of `isPlainObject` that checks if a given `value`
* is an object created by the `Object` constructor, assuming objects created * is an object created by the `Object` constructor, assuming objects created
@@ -1179,10 +1199,15 @@
* // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed) * // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
*/ */
function invert(object) { function invert(object) {
var result = {}; var index = -1,
forOwn(object, function(value, key) { props = keys(object),
result[value] = key; length = props.length,
}); result = {};
while (++index < length) {
var key = props[index];
result[object[key]] = key;
}
return result; return result;
} }
@@ -1684,26 +1709,6 @@
return typeof value == 'undefined'; return typeof value == 'undefined';
} }
/**
* Creates an array composed of the own enumerable property names of `object`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns a new array of property names.
* @example
*
* _.keys({ 'one': 1, 'two': 2, 'three': 3 });
* // => ['one', 'two', 'three'] (order is not guaranteed)
*/
var keys = !nativeKeys ? shimKeys : function(object) {
// avoid iterating over the `prototype` property
return typeof object == 'function' && propertyIsEnumerable.call(object, 'prototype')
? shimKeys(object)
: (isObject(object) ? nativeKeys(object) : []);
};
/** /**
* Merges enumerable properties of the source object(s) into the `destination` * Merges enumerable properties of the source object(s) into the `destination`
* object. Subsequent sources will overwrite propery assignments of previous * object. Subsequent sources will overwrite propery assignments of previous
@@ -1844,10 +1849,15 @@
* // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed) * // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed)
*/ */
function pairs(object) { function pairs(object) {
var result = []; var index = -1,
forOwn(object, function(value, key) { props = keys(object),
result.push([key, value]); length = props.length,
}); result = Array(length);
while (++index < length) {
var key = props[index];
result[index] = [key, object[key]];
}
return result; return result;
} }
@@ -1914,10 +1924,14 @@
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function values(object) { function values(object) {
var result = []; var index = -1,
forOwn(object, function(value) { props = keys(object),
result.push(value); length = props.length,
}); result = Array(length);
while (++index < length) {
result[index] = object[props[index]];
}
return result; return result;
} }