stylesString

Benchmark created on


Setup

const stylesObject = {
	foo: 'foo',
	bar: 'bar',
	baz: null,
	color: undefined
}

function stylesStringEntries(styles) {
    if (!styles || typeof styles !== 'object') return '';
    return Object.entries(styles)
        .filter(([key, value]) => !!value)
        .map(([key, value]) => {
            return `${key}:${value};`;
        })
        .join('');
}

function stylesStringForIn(styles) {
    if (!styles || typeof styles !== 'object') return '';
    let result = '';
    for (let key in styles) {
        if (styles[key]) {
            result += key + ':' + styles[key] + ';';
        }
    }
    return result;
}


function stylesStringReduce(styles) {
    if (!styles || typeof styles !== 'object') return '';
    return Object.keys(styles).reduce((str, key) => {
        const value = styles[key];
        if (value) {
            return str + `${key}:${value};`;
        } else {
        	return str
        }
    }, '');
}

Test runner

Ready to run.

Testing in
TestOps/sec
Object.entries


stylesStringEntries(stylesObject)
ready
ForIn
stylesStringForIn(stylesObject)
ready
Object.keys().Reduce
stylesStringReduce(stylesObject);
ready

Revisions

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