test nook algos

Benchmark created on


Setup

const maps = {
	'1': [],
	'2': [],
	'3': [],
	'4': [],
}
const users = [];
for(let map in maps) {
	for(let j= 0; j < 100; j++) {
		maps[map].push({
			name: 'nook-'+j
		})
	}
}
for(let i = 0; i<25; i++) {
	users.push({
		nookName: Math.floor(Math.random() * 1000),
		nookMap: Math.ceil(Math.random() * 4)
	})
}
const getReadableMapNameFromMapId = (name) => name;

Test runner

Ready to run.

Testing in
TestOps/sec
test current
const getCurrentNookName = ({ allMapNooks, nookMap = "", nookId = "", }) => {
    var _a, _b;
    const name = (_b = (_a = allMapNooks === null || allMapNooks === void 0 ? void 0 : allMapNooks[nookMap]) === null || _a === void 0 ? void 0 : _a[nookId]) === null || _b === void 0 ? void 0 : _b.name;
    const hasSameName = Object.entries(allMapNooks).some(([map, nookInfo]) => map !== nookMap && nookInfo && Object.values(nookInfo).some((item) => name === item.name));
    return hasSameName ? `${name} (${getReadableMapNameFromMapId(nookMap)})` : name;
};

users.forEach(user => {
	getCurrentNookName({ allMapNooks: maps, nookMap: user.nookMap, nookName: user.nookName})
})
ready
test proposal
const buildGetCurrentNookName = (allMapNooks) => {
    const nookNameCount = {};
    Object.entries(allMapNooks).forEach(([map, nookInfo]) => {
        Object.values(nookInfo).forEach(info => {
            nookNameCount[info.name] = nookNameCount[info.name] !== undefined ? nookNameCount[info.name] + 1 : 1;
        });
    });
    return ({ nookMap = "", nookId = "" }) => {
        var _a, _b;
        const name = (_b = (_a = allMapNooks === null || allMapNooks === void 0 ? void 0 : allMapNooks[nookMap]) === null || _a === void 0 ? void 0 : _a[nookId]) === null || _b === void 0 ? void 0 : _b.name;
        const hasSameName = name && (nookNameCount === null || nookNameCount === void 0 ? void 0 : nookNameCount[name]) !== 1;
        return hasSameName ? `${name} (${getReadableMapNameFromMapId(nookMap)})` : name;
    };
};
const getCurrentNookName = buildGetCurrentNookName(maps);
users.forEach(user => {
	getCurrentNookName({ nookMap: user.nookMap, nookId: user.nookName})
})
ready

Revisions

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