merge arrays

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
// USING A SET
function mergeArrays(...arrays) {

    let jointArray = []
    
    arrays.forEach(array => {
        jointArray = [...jointArray, ...array]
    });

    return [...new Set([...jointArray])]

}
ready
// USING ARRAY.FROM() WITH SET
function mergeArrays(...arrays) {
    let jointArray = []
    
    arrays.forEach(array => {
        jointArray = [...jointArray, ...array]
    });
    return Array.from(new Set([...jointArray]))
}
ready
// USING .FILTER()
function mergeArrays(...arrays) {

    let jointArray = []
    
    arrays.forEach(array => {
        jointArray = [...jointArray, ...array]
    })

    const uniqueArray = jointArray.filter((item,index) => jointArray.indexOf(item) === index)

    return uniqueArray
}

ready
// USING .REDUCE()
function mergeArrays(...arrays) {

    let jointArray = []
    
    arrays.forEach(array => {
        jointArray = [...jointArray, ...array]
    })

    const uniqueArray = jointArray.reduce((newArray, item) =>{
        if (newArray.includes(item)){
            return newArray
        } else {
            return [...newArray, item]
        }
    }, [])

    return uniqueArray
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.