jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
// USING A SET
function mergeArraysSet(...arrays) {
let jointArray = []
arrays.forEach(array => {
jointArray = [...jointArray, ...array]
});
return [...new Set([...jointArray])]
}
// USING .FILTER()
function mergeArraysFilter(...arrays) {
let jointArray = []
arrays.forEach(array => {
jointArray = [...jointArray, ...array]
})
const uniqueArray = jointArray.filter((item,index) => jointArray.indexOf(item) === index)
return uniqueArray
}
// USING .REDUCE()
function mergeArraysReduce(...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
}
// USING OBJECT
const mergeUnique = (...arrays) => {
let storedValues = {}
let mergedArray = []
const addValues = (array) => {
array.forEach(item => {
if(!storedValues[item.toString()]) {
mergedArray.push(item)
storedValues[item.toString()] = item
}
})
}
arrays.forEach(array => addValues(array))
return mergedArray
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Using .filter() |
| ready |
Using object |
| ready |
Using sets |
| ready |
Using .reduce() |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.