Javascript array_count_values function I am using an array_count_values function since I have translated the code from php to javascript and javascript does not have this function, but not sure on the performance of it. I'm sure it can be faster. It currently takes 50ms for 32000 iterations each time it is called.

I have one that returns a simple array:

function array_count_values(arr) {//returns array
    let a = [], prev;
    arr.sort();
    for ( let i = 0; i &lt; arr.length; i++ ) {
        if ( arr[i] !== prev ) {
            a.push(1)
        } else {
            a[a.length-1]++;
        }
        prev = arr[i];
    }
    return a;
}


And another that returns an object:

function array_cnt_values(arr) {//returns object
    let a = [], prev;
    arr.sort();
    for ( let i = 0; i &lt; arr.length; i++ ) {
        if ( arr[i] !== prev ) {
            a[arr[i]] = 1
        } else {
            a[arr[i]]++;
        }
        prev = arr[i];
    }
    return a;
}
Your first function just gives an array of counts, without anyway to tell what each of them are counting.

Your second function is better, as the index shows what's being counted. You can use the index to see if it already exists, which means you don't have to sort your array.

if( a[arr[i]] )


This works fine if all values are positive integers. But what if you had strings? It would still work, but you couldn't loop through the result easily. You might also run into problems if you have values equal to existing array properties like length.

Instead you can use a Map, which is similar to the associative array in php.

function array_count_values(arr) {
    let a = new Map();
    for ( let i = 0; i &lt; arr.length; i++ ) {
        if ( a.has(arr[i]) ) {
            a.set(arr[i], a.get(arr[i])+1)
        } else {
            a.set(arr[i], 1)
        }
    }
    return a;
}