Reducing iterations (v2)

Revision 2 of this benchmark created on


Setup

var req1 = {
  data: []
}
for (let i of Array(1000).keys()) {
  req1.data.push({ id: i })
}
var req2 = {
  data: []
}
for (let i of Array(1000).keys()) {
  req2.data.push({ id: i })
}
var responses = [req1, req2]

Test runner

Ready to run.

Testing in
TestOps/sec
Current approach
const data = responses.reduce((result, current) => [...result, ...current.data],
  [],
);
const dbNodes = {};
Object.values(data).forEach((dbNode) => {
  dbNodes[dbNode.id] = { ...dbNode };
});
ready
Consolidated approach
const dbNodes = responses.reduce((acc, { data }) => {
  data.forEach(node => {
  	acc[node.id] = { ...node }
  })
  return acc
}, {})
ready
Old school for loops
const dbNodes = {}
let node
for (let i = 0, len = responses.length; i < len; i++) {
  let { data } = responses[i]
  for (let j = 0, jLen = data.length; j < jLen; j++) {
    node = { ...data[j] }
    dbNodes[node.id] = node
  }
}
ready

Revisions

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