Test Es6 reduce

Benchmark created by bZichett on


Setup

let apiUsers = []
  for (let i = 1; i <= 400; i++) {
      apiUsers.push({id: i.toString(), name: "name" + i})
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Fast
function parseUsers6(apiUsers) {
    return apiUsers.reduce(
        (acc, user) => {
            acc.items[user.id] = user
            acc.keys.push(user.id)
            return acc
        },
        {
            items: {},
            keys: []
        }
    )
}

parseUsers6(apiUsers)
ready
...
function parseUsers1(apiUsers) {
    return {
      items: apiUsers.reduce(
        (acc, user) => ({
          ...acc,
          [user.id]: user
        }),
        {}
      ),
      keys: apiUsers.map(user => user.id)
    }
}

parseUsers1(apiUsers)
ready
assign
function parseUsersAssign(apiUsers) {
    return {
      items: apiUsers.reduce(
        (acc, user) => Object.assign(acc, {
            [user.id]: user
          })
         ,
        {}
      ),
      keys: apiUsers.map(user => user.id)
    }
}

parseUsersAssign(apiUsers)
ready

Revisions

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

  • Revision 1: published by bZichett on