-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Use array.length = 0 instead of goog.array.clear #3136
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
Looks good, +1. |
Looks good to me. Please merge. I'd also be in favor of replacing |
+1 for this change. Thanks @fredj. For people's information, this is what /**
* Clears the array.
* @param {goog.array.ArrayLike} arr Array or array like object to clear.
*/
goog.array.clear = function(arr) {
// For non real arrays we don't have the magic length so we delete the
// indices.
if (!goog.isArray(arr)) {
for (var i = arr.length - 1; i >= 0; i--) {
delete arr[i];
}
}
arr.length = 0;
}; It also works for |
The only justification I can imagine for using these functions is if you're dealing with "array like" arguments ( |
Me too.
/**
* Converts an object to an array.
* @param {Array.<T>|goog.array.ArrayLike} object The object to convert to an
* array.
* @return {!Array.<T>} The object converted into an array. If object has a
* length property, every property indexed with a non-negative number
* less than length will be included in the result. If object does not
* have a length property, an empty array will be returned.
* @template T
*/
goog.array.toArray = function(object) {
var length = object.length;
// If length is not a number the following it false. This case is kept for
// backwards compatibility since there are callers that pass objects that are
// not array like.
if (length > 0) {
var rv = new Array(length);
for (var i = 0; i < length; i++) {
rv[i] = object[i];
}
return rv;
}
return [];
}; There is also /**
* Returns a new array from a segment of an array. This is a generic version of
* Array slice. This means that it might work on other objects similar to
* arrays, such as the arguments object.
*
* @param {Array.<T>|goog.array.ArrayLike} arr The array from
* which to copy a segment.
* @param {number} start The index of the first element to copy.
* @param {number=} opt_end The index after the last element to copy.
* @return {!Array.<T>} A new array containing the specified segment of the
* original array.
* @template T
*/
goog.array.slice = function(arr, start, opt_end) {
goog.asserts.assert(arr.length != null);
// passing 1 arg to slice is not the same as passing 2 where the second is
// null or undefined (in that case the second argument is treated as 0).
// we could use slice on the arguments object and then use apply instead of
// testing the length
if (arguments.length <= 2) {
return goog.array.ARRAY_PROTOTYPE_.slice.call(arr, start);
} else {
return goog.array.ARRAY_PROTOTYPE_.slice.call(arr, start, opt_end);
}
}; but if we know we have an |
It sounds like we're in agreement. |
We only have 7 occurrences of |
Use array.length = 0 instead of goog.array.clear
And
goog.object.clear(obj)
should probably changed toobj = {}