Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
Chrome 84
Windows
Desktop
4 years ago
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
Script Preparation code:
AخA
 
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
Tests:
  • Sorted Compare

     
    sorted1.sort()
    let found, index
    for(index = 0; index < sorted1.length; index+=2){
        if(sorted1[index] !== sorted1[index+1]){
            found = sorted1[index]
            break;
        }
    }
  • Object

     
    let tempObj = {}
    array.forEach((v) => {
      if(tempObj[v]) delete tempObj[v]
      else tempObj[v]= 1
    })
  • Map

     
    let tempMap = new Map()
    array.forEach((v) => {
      if(tempMap.has(v)) tempMap.delete(v)
      else tempMap.set(v, 1)
    })
  • Sorted indexOf Splice

     
    let temp = []
    let foundIndex
    sorted2.sort()
    sorted2.forEach((v) => {
      foundIndex = temp.indexOf(v)
      if(foundIndex > -1) temp.splice(foundIndex,1)
      else temp.push(v) 
    })
  • Set Sum

     
    let mySet = new Set([...array])
    let answer = (2*[...mySet.values()].reduce(sum, 0)) - (array.reduce(sum, 0))
  • XOR

     
    let res = array[0];
    for (i = 1; i < array.length; i++) res = res ^ array[i]