快速删除数组元素(在不考虑数组顺序的时候)
/**
* Removes the array item at the specified index.
* It's faster but the order of the array will be changed.
* @method fastRemoveAt
* @param {any[]} array
* @param {Number} index
*/
function fastRemoveAt (array, index) {
var length = array.length;
if (index < 0 || index >= length) {
return;
}
array[index] = array[length - 1];
array.length = length - 1;
}
/**
* Removes the first occurrence of a specific object from the array.
* It's faster but the order of the array will be changed.
* @method fastRemove
* @param {any[]} array
* @param {Number} value
*/
function fastRemove (array, value) {
var index = array.indexOf(value);
if (index >= 0) {
array[index] = array[array.length - 1];
--array.length;
}
}