Array

Instance Methods

map

Time ComplexityECMAV8Test
function map(fn, thisArg) {
    let array = this
    let n = array.length
    let result = []
 
    for (let i = 0; i < n; i++) {
        if (!(i in array)) continue // 跳过空槽
        
        result[i] = fn.call(thisArg, array[i], i, array)
    }
 
    return result
}
 
Array.prototype.map = map

reduce

Time ComplexityECMAV8Test
function reduce(fn, initValue) {
    let array = this
    let n = array.length
    let prevValue = initValue
    if (n === 0 && initValue === undefined) throw new TypeError()
    
    let i = 0
    if (initValue === undefined) {
        for (; i < n; i++) {
            if (!(i in array)) continue // 跳过空槽
            
            prevValue = array[i]
            i++
            break
        }
    }
 
    for (; i < n; i++) {
        if (!(i in array)) continue // 跳过空槽
 
        prevValue = fn(prevValue, array[i], i, array)
    }
 
    return prevValue
}
 
Array.prototype.reduce = reduce

filter

Time ComplexityECMAV8Test
function filter(fn, thisArg) {
    let array = this
    let n = array.length
    let result = []
 
    for (let i = 0; i < n; i++) {
        if (!(i in array)) continue // 跳过空槽
 
        if (fn.call(thisArg, array[i], i, array)) {
            result[result.length] = array[i]
        }
    }
 
    return result
}
 
Array.prototype.filter = filter

forEach

Time ComplexityECMAV8Test
function forEach(fn, thisArg) {
    let array = this
    let n = array.length
 
    for (let i = 0; i < n; i++) {
        if (!(i in array)) continue // 跳过空槽
 
        fn.call(thisArg, array[i], i, array)
    }
}
 
Array.prototype.forEach = forEach

flat

Time ComplexityECMAV8Test
function flat(depth = 1) {
    let array = this
    let n = array.length
    let result = []
 
    for (let i = 0; i < n; i++) {
        if (!(i in array)) continue // 跳过空槽
 
        let val = array[i]
        if (Array.isArray(val) && depth > 0) {
            result.push(...val.flat(depth - 1))
        } else {
            result.push(val)
        }
    }
 
    return result
}
 
Array.prototype.flat = flat

push

Time ComplexityECMAV8Test
function push(...items) {
    let array = this
    let n = array.length
    let k = items.length
 
    for (let i = 0; i < k; i++) {
        array[i + n] = items[i]
    }
 
    array.length = n + k
    return n + k
}
 
Array.prototype.push = push

pop

Time ComplexityECMAV8Test
function pop() {
    let array = this
    let n = array.length
    if (n === 0) return undefined
 
    let last = array[n - 1]
 
    array.length = n - 1
    return last
}
 
Array.prototype.pop = pop

unshift

Time ComplexityECMAV8Test
function unshift(...items) {
    let array = this
    let n = array.length
    let k = items.length
    if (k === 0) return n
 
    for (let i = n; i > 0; i--) {
        array[i + k - 1] = array[i - 1]
    }
    for (let j = 0; j < k; j++) {
        array[j] = items[j]
    }
 
    array.length = n + k
    return n + k
}
 
Array.prototype.unshift = unshift

shift

Time ComplexityECMAV8Test
function shift() {
    let array = this
    let n = array.length
    if (n === 0) return undefined
 
    let first = array[0]
    for (let i = 1; i < n; i++) {
        array[i - 1] = array[i]
    }
 
    array.length = n - 1
    return first
}
 
Array.prototype.shift = shift