Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JavaScript spread operator vs Object.assign vs for-in loop safe performance (lots of properties)
Measure the fastest way to merge objects, without mutation.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser:
Chrome 131
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Using the spread operator
1115269.2 Ops/sec
Using Object.assign
523897.3 Ops/sec
Using For-In loop
477747.6 Ops/sec
Using For-In loop with hasOwnProperty
249836.0 Ops/sec
Script Preparation code:
const firstObject = { sampleData: 'Hello world', id: 1, name: 'Test Object', isActive: true, count: 42, tags: ['test', 'demo', 'sample'], createdAt: new Date(), updatedAt: new Date(), description: 'This is a test object', category: 'general', priority: 'high', value: 99.99, units: 'kg', isVerified: false, owner: 'Aaron', permissions: ['read', 'write'], metadata: { source: 'system', version: '1.0' }, location: { lat: 35.6895, lon: 139.6917 }, dimensions: { width: 10, height: 20, depth: 5 }, status: 'pending', isArchived: false, color: 'blue', weight: 15.5, isEditable: true, notes: ['First note', 'Second note'], history: [{ timestamp: new Date(), action: 'created' }], settings: { theme: 'dark', notifications: true }, lastLogin: new Date(), preferences: { language: 'English', currency: 'USD' }, securityLevel: 'medium', groups: ['admin', 'user'], rating: 4.5, isPremium: false, attributes: ['responsive', 'scalable'], dependencies: ['moduleA', 'moduleB'], transactions: [{ id: 1001, amount: 50.75, date: new Date() }], schedule: { startTime: '08:00', endTime: '17:00' }, version: '1.2.3', compatibility: ['v1', 'v2'], relatedIds: [100, 200, 300], extraInfo: 'Additional details here', warnings: ['Low battery', 'High temperature'], validation: { isValid: true, errors: [] }, approvals: [{ user: 'manager', status: 'approved' }], associatedFiles: ['doc1.pdf', 'image.png'], lastAccessed: new Date(), trends: { growthRate: 2.5, declineRate: 0.8 } }; const secondObject = { moreData: 'foo bar', id: 2, name: 'Another Test Object', isActive: false, count: 24, tags: ['development', 'test'], createdAt: new Date(), updatedAt: new Date(), description: 'Second test object', category: 'specific', priority: 'medium', value: 75.50, units: 'lbs', isVerified: true, owner: 'Developer', permissions: ['read'], metadata: { source: 'user', version: '2.0' }, location: { lat: 51.5074, lon: -0.1278 }, dimensions: { width: 5, height: 15, depth: 8 }, status: 'completed', isArchived: true, color: 'red', weight: 20.1, isEditable: false, notes: ['Important note'], history: [{ timestamp: new Date(), action: 'updated' }], settings: { theme: 'light', notifications: false }, lastLogin: new Date(), preferences: { language: 'French', currency: 'EUR' }, securityLevel: 'high', groups: ['user'], rating: 3.9, isPremium: true, attributes: ['secure', 'efficient'], dependencies: ['moduleX', 'moduleY'], transactions: [{ id: 2002, amount: 120.99, date: new Date() }], schedule: { startTime: '09:00', endTime: '18:00' }, version: '2.4.7', compatibility: ['v2', 'v3'], relatedIds: [400, 500, 600], extraInfo: 'Extended details', warnings: ['Slow network'], validation: { isValid: false, errors: ['Timeout'] }, approvals: [{ user: 'admin', status: 'rejected' }], associatedFiles: ['spreadsheet.xlsx', 'diagram.svg'], lastAccessed: new Date(), trends: { growthRate: 1.9, declineRate: 0.5 } };
Tests:
Using the spread operator
const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const finalObject = Object.assign({}, firstObject, secondObject);
Using For-In loop
const finalObject = {} for (let key in firstObject) { finalObject[key] = firstObject[key]; } for (let key in secondObject) { finalObject[key] = secondObject[key]; }
Using For-In loop with hasOwnProperty
const finalObject = {} for (let key in firstObject) { if (Object.prototype.hasOwnProperty.call(firstObject, key)) { finalObject[key] = firstObject[key]; } } for (let key in secondObject) { if (Object.prototype.hasOwnProperty.call(secondObject, key)) { finalObject[key] = secondObject[key]; } }