Get unique items with reduce - 2 approaches

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Approach A
[{ x: 'a' }, { x: 'b' }, { x: 'a' }, { x: 'c' }].reduce((prev, curr) => {
    return prev.find(en => en.x === curr.x)
        ? [...prev]
        : [...prev, curr];
}, [])
ready
Approach B
[{ x: 'a' }, { x: 'b' }, { x: 'a' }, { x: 'c' }].reduce((prev, curr) => {
    if (prev.find(en => en.x === curr.x)) {} else {
        prev.push(curr);
    }
    
    return prev;
}, [])
ready

Revisions

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