conditional properties (v3)

Revision 3 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
conditional spread
let condition = true;
const obj = {
	...(condition && {dogs: 'woof'}), 
	...(!condition && {cats: 'meow'})
}
ready
ternary
let condition = true;
const obj = condition ? {dogs: 'woof'} : {cats: 'meow'};
ready
if else
let condition = true;
const obj = {};
if (condition) {
	obj.dogs= 'woof';
}
else {
	obj.cats= 'meow';
}
ready
ternary - multiple comparisons
let condition = true;
const obj = {}; 
condition ? obj.dogs = 'woof' : false;
!condition ? obj.cats = 'meow' : false;
ready
if - multiple comparisons
let condition = true;
const obj = {};
if (condition) {
	obj.dogs = 'woof';
}
if(!condition) {
	obj.cats = 'meow'
}
ready
conditional expr
let condition = true;
const obj = {}; 
void(condition && (obj.dogs = 'woof'));
void(!condition && (obj.cats = 'meow'));
ready
Object assign + ifs
let condition = true;
const obj = {};
if (condition) {
	Object.assign(obj, {dogs: 'woof'});
}
if(!condition) {
	Object.assign(obj, {cats: 'meow'});
}
ready
conditional object assign
let condition = true;
const obj = {};
void(condition && Object.assign(obj, {dogs: 'woof'}));
void(!condition && Object.assign(obj, {cats: 'meow'}));
ready
if spread
let condition = true;
let obj = {};
if (condition) {
	obj = {...{dogs: 'woof'}};
}
else {
	obj = {...{cats: 'meow'}};
}
ready

Revisions

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