path resolver

Benchmark created on


Description

Resolve path from string to object

Setup

function f00(path, value) {
	const index = path.indexOf('.')
    if (index < 0) return { [path]: value }
	const splitted = path.split('.')
	const last = splitted.length - 1
	const obj = {}
	let rel = obj;
	for (let i = 0; i < last; ++i) {
		rel = rel[splitted[i]] = {}
	}
	rel[splitted[last]] = value
	return obj;
}

const m1 = {
	'domain.name.uuid': (value) => ({domain:{name:{uuid:value}}}),
	'domain.uuid': (value) => ({domain:{uuid:value}})
}

function f10(path, value) {
    const index = path.indexOf('.')
    if (index < 0) return { [path]: value }
    return m1[path](value)
}

function f11(path, value) {
    return path.indexOf('.') < 0 ? { [path]: value } : m1[path](value)
}

const m2 = {
	'domain': (value) => ({domain:value}),
	'user': (value) => ({user:value}),
	'domain.name.uuid': (value) => ({domain:{name:{uuid:value}}}),
	'domain.uuid': (value) => ({domain:{uuid:value}})
}

function f20(path, value) {
    return m2[path](value)
}

function f21(path, value) {
	const m21 = {
	'domain': (value) => ({domain:value}),
	'user': (value) => ({user:value}),
	'domain.name.uuid': (value) => ({domain:{name:{uuid:value}}}),
	'domain.uuid': (value) => ({domain:{uuid:value}})
}
    return m21[path](value)
}

function f22(path, value) {
	return {
	'domain': (value) => ({domain:value}),
	'user': (value) => ({user:value}),
	'domain.name.uuid': (value) => ({domain:{name:{uuid:value}}}),
	'domain.uuid': (value) => ({domain:{uuid:value}})
}[path](value)
}

Test runner

Ready to run.

Testing in
TestOps/sec
Function 00 (without map)
f00('domain.name.uuid', 777)
f00('domain.uuid', 777)
f00('domain', 777)
f00('user', 777)
ready
Function 10 (use partial map, long code)
f10('domain.name.uuid', 777)
f10('domain.uuid', 777)
f10('domain', 777)
f10('user', 777)
ready
Function 11 (use partial map, short code)
f11('domain.name.uuid', 777)
f11('domain.uuid', 777)
f11('domain', 777)
f11('user', 777)
ready
Function 20 (use full map, const global)
f20('domain.name.uuid', 777)
f20('domain.uuid', 777)
f20('domain', 777)
f20('user', 777)
ready
Function 21 (use full map, const local)
f21('domain.name.uuid', 777)
f21('domain.uuid', 777)
f21('domain', 777)
f21('user', 777)
ready
Function 22 (use full map, local)
f22('domain.name.uuid', 777)
f22('domain.uuid', 777)
f22('domain', 777)
f22('user', 777)
ready

Revisions

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