Script Preparation code:
AخA
 
function generateId(len) {
    return [...Array(len)]
        .map(i => (~~(Math.random() * 36)).toString(36))
        .join("");
}
Tests:
  • Reduce

    x
     
    let data = [];
    for (let i = 0; i < 10000; i++) {
        data.push({
            id: generateId(4)
        });
    }
    const newDataReduce = Object.keys(data.reduce((acc, cur) => {
        if(acc[cur.id]) return acc;
        acc[cur.id] = true;
        return acc;
    }, {}));
  • Set Map

     
    let data = [];
    for (let i = 0; i < 10000; i++) {
        data.push({
            id: generateId(4)
        });
    }
    const newDataSet = Array.from(new Set(data.map(f => f.id)));
  • Set Simple For

     
    let data = [];
    for (let i = 0; i < 10000; i++) {
        data.push({
            id: generateId(4)
        });
    }
    const newDataSimpleFor = new Set();
    for(let i = 0; i < data.length; i++){
        if(newDataSimpleFor.has(data[i].id)) continue;
        newDataSimpleFor.add(data[i].id);
    }
    const res = Array.from(newDataSimpleFor);
  • Set For Of

     
    let data = [];
    for (let i = 0; i < 10000; i++) {
        data.push({
            id: generateId(4)
        });
    }
    const newDataForOf = new Set();
    for(let dp of data){
        if(newDataForOf.has(dp.id)) continue;
        newDataForOf.add(dp.id);
    }
    const res2 = Array.from(newDataForOf);
  • Set For Each

     
    let data = [];
    for (let i = 0; i < 10000; i++) {
        data.push({
            id: generateId(4)
        });
    }
    const newDataForEach = new Set();
    data.forEach(dp => {
        if(newDataForEach.has(dp.id)) return;
        newDataForEach.add(dp.id);
    });
    const res3 = Array.from(newDataForEach);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Reduce
    Set Map
    Set Simple For
    Set For Of
    Set For Each

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 4 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4243.0 Safari/537.36
Chrome 87 on Windows
View result in a separate tab
Test name Executions per second
Reduce 26.6 Ops/sec
Set Map 29.8 Ops/sec
Set Simple For 30.8 Ops/sec
Set For Of 31.0 Ops/sec
Set For Each 31.3 Ops/sec