function flat3(array) {
if (!Array.isArray(array)) return
const res = []
let target = [...array]
while (target.length > 0) {
const pop = target.pop()
if (Array.isArray(pop)) {
target.push(...pop)
} else {
res.push(pop)
}
}
return res
}
function flat4(array) {
if (!Array.isArray(array)) return
const res = []
let target = [...array]
while (target.length > 0) {
const shift = target.shift()
if (Array.isArray(shift)) {
target.push(...shift)
} else {
res.push(shift)
}
}
return res
}
按理说应该第二种广度优先速度更快,为何是第一种更快呢