mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
refactor(utils): improve filterInPlace performance with optimized loop structure
This commit is contained in:
parent
bc38403daf
commit
57f4e5738f
@ -14,21 +14,19 @@ export function toArray<T> (value: T | T[]): T[] {
|
||||
* and standard filterInPlace implementations.
|
||||
*/
|
||||
export function filterInPlace<T> (array: T[], predicate: (item: T, index: number, arr: T[]) => unknown) {
|
||||
let i = array.length
|
||||
const len = array.length
|
||||
|
||||
if (i <= 16) {
|
||||
while (i--) {
|
||||
if (len <= 16) {
|
||||
for (let i = len; i--; i >= 0) {
|
||||
if (!predicate(array[i]!, i, array)) {
|
||||
array.splice(i, 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (i--) {
|
||||
for (let i = len; i--; i >= 0) {
|
||||
if (!predicate(array[i]!, i, array)) {
|
||||
const last = --array.length
|
||||
if (i < last) {
|
||||
array[i] = array[last] as T
|
||||
}
|
||||
if (i < last) { array[i] = array[last]! }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user