Get unique items with reduce - 2 approaches (v2)

Revision 2 of this 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.concat(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.