Test name | Executions per second |
---|---|
Sorted Compare | 19.5 Ops/sec |
Object | 63.1 Ops/sec |
Map | 81.9 Ops/sec |
Sorted indexOf Splice | 18.1 Ops/sec |
UnSorted indexOf Splice | 0.5 Ops/sec |
var length = 100000;
let added = [];
var array = new Array(length / 2).fill().map(() => {
let number = Math.round(Math.random() * length);
do {number = Math.round(Math.random() * length)} while (added.includes(number));
added.push(number);
return number;
});
array = array.concat(array);
array.splice(Math.round(Math.random() * length), 1);
var sorted1 = [array]
var sorted2 = [array]
var sum = ((a, b) => a + b)
var i
sorted1.sort()
let found, index
for(index = 0; index < sorted1.length; index+=2){
if(sorted1[index] !== sorted1[index+1]){
found = sorted1[index]
break;
}
}
let tempObj = {}
array.forEach((v) => {
if(tempObj[v]) delete tempObj[v]
else tempObj[v]= 1
})
let tempMap = new Map()
array.forEach((v) => {
if(tempMap.has(v)) tempMap.delete(v)
else tempMap.set(v, 1)
})
let temp = []
let foundIndex
sorted2.sort()
sorted2.forEach((v) => {
foundIndex = temp.indexOf(v)
if(foundIndex > -1) temp.splice(foundIndex,1)
else temp.push(v)
})
let mySet = new Set([array])
let answer = (2*[mySet.values()].reduce(sum, 0)) - (array.reduce(sum, 0))
let res = array[0];
for (i = 1; i < array.length; i++) res = res ^ array[i]