Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Transform array to object
(version: 3)
Various ways to convert array to objects
Comparing performance of:
lodash/keyBy vs keyBy with native reduce vs keyBy using native reduce and spread operator
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
var data = Array.from({length: 1000}, (v, i) => ({ name: `${i + 1}`, value: i + 1})); var keyBy = (array, fn) => { return array.reduce((acc, value) => { acc[fn(value)] = value; return acc; }, {}); }; var keyByUsingSpread = (array, fn) => { return array.reduce((acc, value) => { return { ...acc, [fn(value)]: value }; }, {}); };
Tests:
lodash/keyBy
_.keyBy(data, (v) => v.name);
keyBy with native reduce
keyBy(data, (v) => v.name);
keyBy using native reduce and spread operator
keyByUsingSpread(data, (v) => v.name);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash/keyBy
keyBy with native reduce
keyBy using native reduce and spread operator
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:
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
lodash mapValues vs vanilla Object.keys foreach vs lodash reduce
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
Comments
Confirm delete:
Do you really want to delete benchmark?