sort vs array on long numbers (searching for sort win) (v2)

Revision 2 of this benchmark created on


Setup

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}

function shuffleDigits(number) {
    let digits = shuffleArray(number.toString().split(''));
    return parseInt(digits.join(''));
}

const size = 1000;
const groupSize = 100;
const MAX_NUMBER = Number.MAX_SAFE_INTEGER;
let input = [];
for (let i = 10000; i < 10000 + size; i += 1) {
	const num = Math.floor(Math.random() * MAX_NUMBER);
	for (let j = 0; j < groupSize; j += 1) {
		input.push(shuffleDigits(num));
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
array
function digitPermutation(arr) {
    const anagrams = {};

    for (let curr of arr) {
        let acc = [];

        for (let dig of curr.toString()) {
            acc[dig] = (acc[dig] || 0) + 1;
        }

        let res = '';
        for (let j = 1; j < acc.length; j++) {
            res += j.toString().repeat(acc[j])
        }

        if (!anagrams[res]) anagrams[res] = [];

        anagrams[res].push(curr)
    }

    return Object.values(anagrams);
}

digitPermutation(input)
ready
array - no key resize
function digitPermutation(arr) {
    const anagrams = {};

    for (let curr of arr) {
        let acc = new Array(10).fill(0);

        for (let dig of curr.toString()) {
            acc[dig] += 1;
        }

        let res = '';
        for (let j = 1; j < acc.length; j++) {
            res += j.toString().repeat(acc[j])
        }

        if (!anagrams[res]) anagrams[res] = [];

        anagrams[res].push(curr)
    }

    return Object.values(anagrams);
}

digitPermutation(input)
ready
array - no key resize & key join
function digitPermutation(arr) {
    const anagrams = {};

    for (let curr of arr) {
        let acc = new Array(10).fill(0);

        for (let dig of curr.toString()) {
            acc[dig] += 1;
        }

		acc[0] = 0;

        let res = acc.join('_');

        if (!anagrams[res]) anagrams[res] = [];

        anagrams[res].push(curr)
    }

    return Object.values(anagrams);
}

digitPermutation(input)
ready
sort
function digitPermutation(arr) {
    const anagrams = {};

    for (let curr of arr) {
        let res = Number(curr.toString().split('').sort().join(''));

        if (!anagrams[res]) anagrams[res] = [];

        anagrams[res].push(curr)
    }

    return Object.values(anagrams);
}

digitPermutation(input)
ready
sort - dummy zero remove
function digitPermutation(arr) {
    const anagrams = {};

    for (let curr of arr) {
        let res = curr.toString().split('').filter(d => d !== '0').sort().join('');

        if (!anagrams[res]) anagrams[res] = [];

        anagrams[res].push(curr)
    }

    return Object.values(anagrams);
}

digitPermutation(input)
ready

Revisions

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