Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object to Arrayss3
(version: 0)
Converts object to array.
Comparing performance of:
Current Method vs My Method (added empty check) vs Yashish Method (added empty check)
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function obj2Array2(obj, prefix) { var arr = []; _.forIn(obj, function (value, key) { if (_.isObject(value) && !_.isEmpty(value)) { arr = _.concat(arr, obj2Array2(value, key)); } else { arr.push(key); try { arr.push(JSON.stringify(value)); } catch (e) { arr.push(value); } } }); if (_.isString(prefix) && !_.isEmpty(prefix)) { return _.map(arr, function (arg, index) { if (index % 2 === 0) { return `${prefix}.${arg}`; } return arg; }); } return arr; } function obj2Array (object, prefix) { var arr = []; Object.keys(object).forEach(function (key) { const value = object[key]; if (typeof value === 'object' && Object.keys(value).length !== 0) { let prefixedKey = (prefix && `${prefix}.${key}`) || key, returnedArray = obj2Array(value, prefixedKey); arr = arr.concat(returnedArray); return; } arr.push((prefix && `${prefix}.${key}`) || key); arr.push(JSON.stringify(value)); }); return arr; } function obj2Array3(obj, prefix, arr = []) { if (!obj) { return arr; } const usePrefix = prefix && typeof prefix === 'string'; usePrefix && (prefix += '.'); Object.keys(obj).forEach(function (key) { const value = obj[key]; usePrefix && (key = prefix + key); if (typeof value === 'object' && Object.keys(value).length !== 0) { obj2Array3(value, key, arr); } else { arr.push(key, JSON.stringify(value)); } }); return arr; } var objToTest = { user: { id: 123, data: { username: 'marvin', email: 'marvin@heartofgold.ship' }, tags: ['robot', 'depressed', 'brain the size of a planet'] } }
Tests:
Current Method
obj2Array2(objToTest);
My Method (added empty check)
obj2Array(objToTest);
Yashish Method (added empty check)
obj2Array3(objToTest);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Current Method
My Method (added empty check)
Yashish Method (added empty check)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Two Arrays to Object #3
Object keys vs Array map v2
Array of Objects to Object with keys Object.fromEntries v Object.assign
Map convert
For in vs Object.*.forEach vs Object.values 2
Comments
Confirm delete:
Do you really want to delete benchmark?