var obj = {};
for (i = 1000; i > 0; i--) {
if (i%2 == 0) {
obj[i] = 'String';
} else {
obj[i] = [ 'Array of String', 'Array of String' ];
}
}
function ClassList(collection={}) {
const classNames = {};
const keys = Object.keys(collection);
keys.forEach((key) => {
const innerCollection = collection[key];
const collectionCheck = Object.prototype.toString.call(innerCollection);
switch (collectionCheck) {
case '[object Object]': {
const innerKeys = Object.keys(innerCollection);
if (innerKeys.length === 0) {
classNames[key] = '';
break;
}
let classList = [];
innerKeys.forEach((innerKey) => {
if (!innerCollection[innerKey] || !innerCollection[innerKey].length) {
return;
}
const innerCollectionCheck = Object.prototype.toString.call(innerCollection[innerKey]);
switch (innerCollectionCheck) {
case '[object Array]':
classList.push(innerCollection[innerKey]);
break;
case '[object String]':
classList.push(innerCollection[innerKey]);
break;
}
});
classNames[key] = classList.reduce((acc, cl) => (acc += cl + ' '), '');
break;
}
case '[object Array]':
classNames[key] = innerCollection.reduce((acc, cl) => (acc += cl + ' '), '');
break;
case '[object String]':
classNames[key] = innerCollection;
break;
}
});
return classNames;
}
ClassList( obj );
function ClassList(collection={}) {
const classNames = {};
const keys = Object.keys(collection);
keys.forEach((key) => {
const innerCollection = collection[key];
const collectionCheck = Object.prototype.toString.call(innerCollection);
switch (collectionCheck) {
case '[object Object]': {
const innerKeys = Object.keys(innerCollection);
if (innerKeys.length === 0) {
classNames[key] = '';
break;
}
let classList = [];
innerKeys.forEach((innerKey) => {
if (!innerCollection[innerKey] || !innerCollection[innerKey].length) {
return;
}
const innerCollectionCheck = Object.prototype.toString.call(innerCollection[innerKey]);
switch (innerCollectionCheck) {
case '[object Array]':
classList.push(innerCollection[innerKey]);
break;
case '[object String]':
classList.push(innerCollection[innerKey]);
break;
}
});
classNames[key] = classList.join(' ');
break;
}
case '[object Array]':
classNames[key] = innerCollection.join(' ');
break;
case '[object String]':
classNames[key] = innerCollection;
break;
}
});
return classNames;
}
ClassList( obj );
function ClassList(collection={}) {
const classNames = {};
const keys = Object.keys(collection);
for (const key of keys) {
if ( Object.prototype.hasOwnProperty.call(collection, key) ) {
const innerCollection = collection[key];
const collectionCheck = Object.prototype.toString.call(innerCollection);
switch (collectionCheck) {
case '[object Object]': {
const innerKeys = Object.keys(innerCollection);
if (innerKeys.length === 0) {
classNames[key] = '';
break;
}
let classList = [];
innerKeysLoop: for (const innerKey of innerKeys) {
if (!innerCollection[innerKey] || !innerCollection[innerKey].length) {
continue innerKeysLoop;
}
const innerCollectionCheck = Object.prototype.toString.call(innerCollection[innerKey]);
switch (innerCollectionCheck) {
case '[object Array]':
classList.push(innerCollection[innerKey]);
break;
case '[object String]':
classList.push(innerCollection[innerKey]);
break;
}
}
classNames[key] = classList.reduce((acc, cl) => (acc += cl + ' '), '');
break;
}
case '[object Array]':
classNames[key] = innerCollection.reduce((acc, cl) => (acc += cl + ' '), '');
break;
case '[object String]':
classNames[key] = innerCollection;
break;
}
}
}
return classNames;
}
ClassList( obj );
function ClassList(collection={}) {
const classNames = {};
const keys = Object.keys(collection);
for (const key of keys) {
if ( Object.prototype.hasOwnProperty.call(collection, key) ) {
const innerCollection = collection[key];
const collectionCheck = Object.prototype.toString.call(innerCollection);
switch (collectionCheck) {
case '[object Object]': {
const innerKeys = Object.keys(innerCollection);
if (innerKeys.length === 0) {
classNames[key] = '';
break;
}
let classList = [];
innerKeysLoop: for (const innerKey of innerKeys) {
if (!innerCollection[innerKey] || !innerCollection[innerKey].length) {
continue innerKeysLoop;
}
const innerCollectionCheck = Object.prototype.toString.call(innerCollection[innerKey]);
switch (innerCollectionCheck) {
case '[object Array]':
classList.push(innerCollection[innerKey]);
break;
case '[object String]':
classList.push(innerCollection[innerKey]);
break;
}
}
classNames[key] = classList.join(' ');
break;
}
case '[object Array]':
classNames[key] = innerCollection.join(' ');
break;
case '[object String]':
classNames[key] = innerCollection;
break;
}
}
}
return classNames;
}
ClassList( obj );
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach & concatenate reduce | |
forEach & concatenate join | |
for .. of & concatenate reduce | |
for .. of & concatenate join |
Test name | Executions per second |
---|---|
forEach & concatenate reduce | 2063.4 Ops/sec |
forEach & concatenate join | 1931.7 Ops/sec |
for .. of & concatenate reduce | 1930.4 Ops/sec |
for .. of & concatenate join | 1938.7 Ops/sec |
It looks like you've shared some benchmarking results!
To summarize, there are four tests with different browser versions and device platforms. The test names are:
forEach & concatenate reduce
for .. of & concatenate join
for .. of & concatenate reduce
(second run)for .. of & concatenate join
(second run)The executions per second for each test vary, but it appears that the first test has the highest execution rate.
Here are some observations:
forEach
seem to be faster than those with for .. of
.concatenate join
seem to be slower than those with concatenate reduce
.However, without more context or additional information, it's difficult to provide a definitive answer. Could you please share the specific questions or requirements you'd like me to help with?