Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance 2123 123123
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Empty object assign
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var firstObject = { sampleData: 'Hello world', sampleData2: 'Hello world', sampleData3: 'Hello world',sampleData4: 'Hello world' }; var secondObject = { moreData: 'foo bar',moreData2: 'foo bar',moreData3: 'foo bar',moreData4: 'foo bar' }; var unit = {"name":"New unit gg sfsd wefwe f ss2223 g","unitType":1,"users":[],"centralCheckLists":[],"unitScheduling":{"dailyActivities":[{"activeDayOfWeek":1,"activeFromHour":"06:00","activeToHour":"22:00","endOfDay":"00:00","activeOffset":false},{"activeDayOfWeek":2,"activeFromHour":"06:00","activeToHour":"22:00","endOfDay":"00:00","activeOffset":false},{"activeDayOfWeek":3,"activeFromHour":"06:00","activeToHour":"22:00","endOfDay":"00:00","activeOffset":false},{"activeDayOfWeek":4,"activeFromHour":"11:00","activeToHour":"22:00","endOfDay":"00:00","activeOffset":false},{"activeDayOfWeek":5,"activeFromHour":"23:00","activeToHour":"22:00","endOfDay":"00:00","activeOffset":false}],"excludedDates":[]},"documentType":6,"revisionId":4,"id":"b99bff7d-9f54-42ea-8b0a-a02827a58d3b","tenantId":"11211c12-455e-4c2f-827b-beb180e33b54","createdByUserId":null,"lastModifiedUserId":null,"isDeleted":false,"_etag":"\"02007ee4-0000-0d00-0000-5d550fdc0000\""}; var data = {name:"New name", unitScheduling:{}}; function generateFields(user, data){ const baseUnitScheduling = { excludedDates: [], dailyActivities: ([1, 2, 3, 4, 5]).map(day => ({ activeDayOfWeek: day, activeToHour: '22:00', activeFromHour: '06:00', activeOffset: false, })), }; const unitScheduling = user.unitScheduling || data.unitScheduling || baseUnitScheduling; unitScheduling.dailyActivities = unitScheduling.dailyActivities || baseUnitScheduling.dailyActivities; unitScheduling.excludedDates = unitScheduling.excludedDates || baseUnitScheduling.excludedDates; return { unitScheduling, users: user.users || data.users || [], centralCheckLists: user.centralCheckLists || data.centralCheckLists || [], }; }
Tests:
Using the spread operator
const finalObject ={ ...unit, ...data, ...generateFields(unit, data), }
Using Object.assign
const finalObject = Object.assign(unit, data, generateFields(unit, data));
Empty object assign
const finalObject = Object.assign({}, unit, data, generateFields(unit, data))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Empty object assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare the performance of three approaches: using the spread operator (`...`), `Object.assign`, and an "empty object assign" method. The test case creates a complex object by merging multiple objects using these methods and measures the execution time. **Test Cases** There are three individual test cases: 1. **Using the Spread Operator**: This approach uses the spread operator (`...`) to merge the `unit`, `data`, and `generateFields(unit, data)` objects into a single object. 2. **Using Object.assign**: This approach uses the `Object.assign()` method to merge the `unit`, `data`, and `generateFields(unit, data)` objects into a single object. 3. **Empty Object Assign**: This approach creates an empty object (`{}`) and then assigns the `unit`, `data`, and `generateFields(unit, data)` objects to it. **Comparison** The benchmark compares the performance of these three approaches by measuring their execution time for a set number of executions (in this case, 1000 executions). **Pros and Cons** Here's a brief overview of each approach: 1. **Using the Spread Operator (`...`)**: * Pros: concise, readable, and efficient. * Cons: may have performance implications due to object creation and merging. 2. **Using Object.assign()**: * Pros: widely supported, fast, and efficient. * Cons: can be less readable than other approaches, especially for complex objects. 3. **Empty Object Assign**: * Pros: simple, lightweight, and efficient. * Cons: may have performance implications due to object creation and merging. **Library** The `generateFields()` function is a custom-built library that generates a nested object with certain properties. Its purpose is not explicitly stated in the benchmark definition, but it appears to be a utility function for creating complex objects. **Special JS Feature or Syntax** There's no special JavaScript feature or syntax mentioned in this benchmark. The use of the spread operator (`...`) and `Object.assign()` are standard features of modern JavaScript. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few: 1. **ES6 Object Spread Operator**: Instead of using the spread operator (`...`), you could use the ES6 object spread operator syntax: `{ ...unit, ...data, ...generateFields(unit, data) }`. 2. **JSON Merge Patching**: You could use JSON merge patching libraries like `json-merge-patch` or `json-merge` to merge objects in a more efficient and scalable way. 3. **Functional Programming Approaches**: You could explore functional programming approaches like using `reduce()` or `forEach()` to merge objects, which can be more concise and readable. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and coding style preferences.
Related benchmarks:
JavaScript spread operator vs Object.assign performance 2021 (v2)
JavaScript spread operator vs Object.assign performance with New Object Twice
JavaScript spread operator vs Object.assign performance 2222
JavaScript spread operator vs Object.assign performance (create new objects)
JavaScript optimized spread operator vs Object.assign performance
Comments
Confirm delete:
Do you really want to delete benchmark?