func test

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
iterator build
function nestIterative(data, parent = '_index', link = 'parent') {
  const map = {}; // Map to store items by their ID
  const result = []; // Array to hold the root items

  // Create the map and initialize children arrays
  for (const item of data) {
    map[item.id] = { ...item, children: [] };
  }

  // Build the nested structure
  for (const item of data) {
    if (item[link] === parent) {
      result.push(map[item.id]); // Root items
    } else if (map[item[link]]) {
      map[item[link]].children.push(map[item.id]); // Add as child
    }
  }

  return result;
}
ready
recurse
const nest = (data, parent = '_index', link = 'parent') =>
      data
        .filter((item) => item[link] === parent)
        .map((item) => ({ ...item, children: nest(data, item.id) }))
ready

Revisions

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