Remove array and object pools.

This commit is contained in:
John-David Dalton
2014-02-21 22:26:17 -08:00
parent 24240bfba4
commit 6854fee73e
7 changed files with 153 additions and 384 deletions

101
lodash.js
View File

@@ -22,9 +22,6 @@
/** Used as the size when optimizations are enabled for arrays */
var LARGE_ARRAY_SIZE = 40;
/** Used as the max size of the `arrayPool` and `objectPool` */
var MAX_POOL_SIZE = 40;
/** Used as the semantic version number */
var version = '2.4.1';
@@ -84,10 +81,6 @@
'\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
);
/** Used to pool arrays and objects used internally */
var arrayPool = [],
objectPool = [];
/** Used to assign default `context` object properties */
var contextProps = [
'Array', 'Boolean', 'Date', 'Error', 'Function', 'Math', 'Number', 'Object',
@@ -371,30 +364,6 @@
return '\\' + stringEscapes[match];
}
/**
* Gets an array from the array pool or creates a new one if the pool is empty.
*
* @private
* @returns {Array} The array from the pool.
*/
function getArray() {
return arrayPool.pop() || [];
}
/**
* Gets an object from the object pool or creates a new one if the pool is empty.
*
* @private
* @returns {Object} The object from the pool.
*/
function getObject() {
return objectPool.pop() || {
'criteria': null,
'index': 0,
'value': null
};
}
/**
* Checks if `value` is a DOM node in IE < 9.
*
@@ -408,32 +377,6 @@
return typeof value.toString != 'function' && typeof (value + '') == 'string';
}
/**
* Releases `array` back to the array pool.
*
* @private
* @param {Array} array The array to release.
*/
function releaseArray(array) {
array.length = 0;
if (arrayPool.length < MAX_POOL_SIZE) {
arrayPool.push(array);
}
}
/**
* Releases `object` back to the object pool.
*
* @private
* @param {Object} object The object to release.
*/
function releaseObject(object) {
object.criteria = object.value = null;
if (objectPool.length < MAX_POOL_SIZE) {
objectPool.push(object);
}
}
/**
* A fallback implementation of `trim` to remove leading and trailing
* whitespace or specified characters from `string`.
@@ -1150,8 +1093,8 @@
if (isDeep) {
// check for circular references and return corresponding clone
var initedStack = !stackA;
stackA || (stackA = getArray());
stackB || (stackB = getArray());
stackA || (stackA = []);
stackB || (stackB = []);
var length = stackA.length;
while (length--) {
@@ -1187,10 +1130,6 @@
result[key] = baseClone(objValue, isDeep, callback, stackA, stackB);
});
if (initedStack) {
releaseArray(stackA);
releaseArray(stackB);
}
return result;
}
@@ -1612,8 +1551,8 @@
// the algorithm for detecting cyclic structures is adapted from ES 5.1
// section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3)
var initedStack = !stackA;
stackA || (stackA = getArray());
stackB || (stackB = getArray());
stackA || (stackA = []);
stackB || (stackB = []);
var length = stackA.length;
while (length--) {
@@ -1678,10 +1617,6 @@
stackA.pop();
stackB.pop();
if (initedStack) {
releaseArray(stackA);
releaseArray(stackB);
}
return result;
}
@@ -1784,7 +1719,7 @@
var seen = createCache();
indexOf = cacheIndexOf;
} else {
seen = (!isSorted && callback) ? getArray() : result;
seen = (callback && !isSorted) ? [] : result;
}
while (++index < length) {
var value = array[index],
@@ -1802,9 +1737,6 @@
result.push(value);
}
}
if (!isLarge && callback) {
releaseArray(seen);
}
return result;
}
@@ -2630,10 +2562,10 @@
var args = [],
argsIndex = -1,
argsLength = arguments.length,
caches = getArray(),
caches = [],
indexOf = getIndexOf(),
largePrereq = createCache && indexOf === baseIndexOf,
seen = getArray();
seen = [];
while (++argsIndex < argsLength) {
var value = arguments[argsIndex];
@@ -2665,8 +2597,6 @@
result.push(value);
}
}
releaseArray(caches);
releaseArray(seen);
return result;
}
@@ -3247,7 +3177,7 @@
* @memberOf _
* @alias unzip
* @category Arrays
* @param {...Array} [array] Arrays to process.
* @param {...Array} [array] The arrays to process.
* @returns {Array} Returns a new array of grouped elements.
* @example
*
@@ -4617,18 +4547,13 @@
} else {
criteria = callback(value, key, collection);
}
var object = result[++index] = getObject();
object.criteria = criteria;
object.index = index;
object.value = value;
result[++index] = { 'criteria': criteria, 'index': index, 'value': value };
});
length = result.length;
result.sort(multi ? compareMultipleAscending : compareAscending);
while (length--) {
var object = result[length];
result[length] = object.value;
releaseObject(object);
result[length] = result[length].value;
}
return result;
}
@@ -6493,14 +6418,12 @@
}
var sources = slice(arguments, 1, length),
index = -1,
stackA = getArray(),
stackB = getArray();
stackA = [],
stackB = [];
while (++index < length) {
baseMerge(object, sources[index], callback, stackA, stackB);
}
releaseArray(stackA);
releaseArray(stackB);
return object;
}