Concat vs Push (v3)

Revision 3 of this benchmark created on


Setup

// Create a function to generate large objects
function createLargeObject() {
  return {
    id: Math.random().toString(36).substr(2, 9),
    name: `User ${Math.floor(Math.random() * 10000)}`,
    email: `user${Math.floor(Math.random() * 10000)}@example.com`,
    data: {
      preferences: {
        theme: Math.random() > 0.5 ? 'dark' : 'light',
        notifications: Math.random() > 0.5,
        language: ['en', 'es', 'fr', 'de'][Math.floor(Math.random() * 4)]
      },
      stats: {
        loginCount: Math.floor(Math.random() * 1000),
        lastActive: new Date(Date.now() - Math.random() * 86400000 * 30),
        score: Math.random() * 100
      },
      metadata: {
        createdAt: new Date(Date.now() - Math.random() * 86400000 * 365),
        tags: Array.from({length: Math.floor(Math.random() * 5) + 1}, 
                        () => `tag${Math.floor(Math.random() * 100)}`),
        description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(Math.floor(Math.random() * 5) + 1)
      }
    }
  };
}

// Create the arrays with large objects
let arr1 = [...Array(64_000)].map(() => createLargeObject());
let arr2 = [...Array(64_000)].map(() => createLargeObject());

Test runner

Ready to run.

Testing in
TestOps/sec
concat
let total = [];
total = total.concat(arr1);
total = total.concat(arr2);
ready
push (spread)
let total = [];
total.push(...arr1);
total.push(...arr2);
ready
push (apply)
let total = [];
Array.prototype.push.apply(total, arr1);
Array.prototype.push.apply(total, arr2);
ready

Revisions

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