array text

Benchmark created on


Setup

const singlePost = [
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 1,
      name: "taxonomy-01",
      slug: "taxonomy-01"
    }
  },
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 2,
      name: "taxonomy-02",
      slug: "taxonomy-02"
    }
  }
];

const newData = [];

for (const current of singlePost) {
  let existingPost = newData.find(post => post.id === current.id);

  if (existingPost) {
    existingPost.category.push(current.category); // Add category to existing post
  } else {
    newData.push({ ...current, category: [current.category] }); // Create new post with category array
  }
}

Test runner

Ready to run.

Testing in
TestOps/sec
dd
const singlePost = [
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 1,
      name: "taxonomy-01",
      slug: "taxonomy-01"
    }
  },
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 2,
      name: "taxonomy-02",
      slug: "taxonomy-02"
    }
  }
];

const newData = [];

for (const current of singlePost) {
  let existingPost = newData.find(post => post.id === current.id);

  if (existingPost) {
    existingPost.category.push(current.category); // Add category to existing post
  } else {
    newData.push({ ...current, category: [current.category] }); // Create new post with category array
  }
}
ready
fsf
const singlePost = [
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 1,
      name: "taxonomy-01",
      slug: "taxonomy-01"
    }
  },
  {
    id: 4,
    title: "post 01",
    slug: "slug 01",
    author: {
      id: 1,
      email: "arup@gmail.com"
    },
    category: {
      id: 2,
      name: "taxonomy-02",
      slug: "taxonomy-02"
    }
  }
];

const newData = [];

for (const current of singlePost) {
  let existingPost = newData.find(post => post.id === current.id);

  if (existingPost) {
    existingPost.category.push(current.category); // Add category to existing post
  } else {
    newData.push({ ...current, category: [current.category] }); // Create new post with category array
  }
}
ready

Revisions

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