Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Chrome 100
Linux
Desktop
2 years ago
Test name Executions per second
Javascript Array.reduce/Array.indexOf 91058.1 Ops/sec
Lodash Uniq 262464.8 Ops/sec
function unique(arr) 340386.9 Ops/sec
HTML Preparation code:
AخA
 
1
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
 
var itemsCount = 1e2;
var items = Array.from({ length: itemsCount }, () => Math.floor(Math.random() * itemsCount));
Tests:
  • Javascript Array.reduce/Array.indexOf

     
    items.reduce((list, item) => list.indexOf(item) > -1 ? list : [...list, item], []);
  • Lodash Uniq

     
    _.uniq(items)
  • function unique(arr)

     
    function unique(arr) {
        var hash = {}, result = [];
        for ( var i = 0, l = arr.length; i < l; ++i ) {
            if ( !hash.hasOwnProperty(arr[i]) ) { //it works with objects! in FF, at least
                hash[ arr[i] ] = true;
                result.push(arr[i]);
            }
        }
        return result;
    }
    unique(items)