Array to Object (v2)

Revision 2 of this benchmark created on


Description

Convert an array to an object. I.e. [1,2,3,4] => {1:'foo', 2:'foo', 3:'foo', 4:'foo'}

Preparation HTML

<script  src="https://unpkg.com/rxjs@7.8.1/dist/bundles/rxjs.umd.min.js"></script>

Setup

const items = Array.from({ length: 10_000 }, (_, i) => i)

Test runner

Ready to run.

Testing in
TestOps/sec
Spread
items.reduce((acc, item) => {
  return { ...acc, [item]: 'foo' }
}, {})
ready
AssignDirect
items.reduce((acc, item) => {
  acc[item] = 'foo'
  return acc
}, {})
ready
AssignObject
items.reduce(
  (acc, item) => Object.assign(acc, { [item]: 'foo' }),
  {}
)
ready
FromEntries
Object.fromEntries(items.map((item) => [item, 'foo']))
ready
ForLoop
let obj = {}
for (let i = 0; i < items.length; i++) {
  obj[i] = 'foo'
}
ready
ForOfLoop
let obj2 = {}
for (const i of items) {
  obj2[i] = 'foo'
}
ready
RxJsReduce
const { from, filter, reduce, map, toArray } = rxjs

return from(items)
  .pipe(reduce((acc, i) => (acc[i] = 'foo') && acc, {}))
  .toPromise(null)
ready
FromEntriesIterator
Object.fromEntries(items.map(() => 'foo').entries())
ready

Revisions

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