Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object.assign vs spread (when base object is large) fixed
(version: 0)
Comparing performance of:
object.assign vs spread vs without immutable
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var baseObj = {"field0":"value0","field1":"value1","field2":"value2","field3":"value3","field4":"value4","field5":"value5","field6":"value6","field7":"value7","field8":"value8","field9":"value9","field10":"value10","field11":"value11","field12":"value12","field13":"value13","field14":"value14","field15":"value15","field16":"value16","field17":"value17","field18":"value18","field19":"value19","field20":"value20","field21":"value21","field22":"value22","field23":"value23","field24":"value24","field25":"value25","field26":"value26","field27":"value27","field28":"value28","field29":"value29","field30":"value30","field31":"value31","field32":"value32","field33":"value33","field34":"value34","field35":"value35","field36":"value36","field37":"value37","field38":"value38","field39":"value39","field40":"value40","field41":"value41","field42":"value42","field43":"value43","field44":"value44","field45":"value45","field46":"value46","field47":"value47","field48":"value48","field49":"value49","field50":"value50","field51":"value51","field52":"value52","field53":"value53","field54":"value54","field55":"value55","field56":"value56","field57":"value57","field58":"value58","field59":"value59","field60":"value60","field61":"value61","field62":"value62","field63":"value63","field64":"value64","field65":"value65","field66":"value66","field67":"value67","field68":"value68","field69":"value69","field70":"value70","field71":"value71","field72":"value72","field73":"value73","field74":"value74","field75":"value75","field76":"value76","field77":"value77","field78":"value78","field79":"value79","field80":"value80","field81":"value81","field82":"value82","field83":"value83","field84":"value84","field85":"value85","field86":"value86","field87":"value87","field88":"value88","field89":"value89","field90":"value90","field91":"value91","field92":"value92","field93":"value93","field94":"value94","field95":"value95","field96":"value96","field97":"value97","field98":"value98","field99":"value99"} var newObj = {"n0":"v0","n1":"v0","n2":"v0","n3":"v0","n4":"v0"}
Tests:
object.assign
var c = Object.assign(baseObj, newObj);
spread
var c = {...baseObj, ...newObj}
without immutable
Object.keys(newObj).forEach(key=>(baseObj[key]=newObj[key]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
object.assign
spread
without immutable
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):
Measuring the performance of object assignments in JavaScript is an interesting benchmark. **Benchmark Definition** The benchmark tests three different ways to assign properties to an existing object (`baseObj`) using either the `Object.assign()` method, the spread operator (`...`), or manually iterating over the keys of the `newObj` object and assigning values to `baseObj`. **Options being compared:** 1. **object.assign()**: This is a built-in JavaScript method that creates a new object with the properties of two or more source objects. 2. **Spread operator (`...`)**: This is a shorthand syntax for creating a new object with the properties of an existing object and one or more additional objects. 3. **Manual iteration without immutability**: This approach involves iterating over the keys of `newObj` and assigning values to `baseObj` using direct property access. **Pros and Cons:** * **object.assign()**: * Pros: Simple, readable syntax; well-documented; widely supported. * Cons: Can be slower for large objects due to the overhead of method call and object creation. * **Spread operator (`...`)**: * Pros: Efficient for creating new objects with similar properties; concise syntax. * Cons: May not work as expected if `baseObj` is not an object (e.g., if it's a string or number); can be slower due to the overhead of object creation. * **Manual iteration without immutability**: * Pros: Can be faster for large objects since no overhead is created; controlled iterations ensure data integrity. * Cons: More verbose syntax; requires more effort to maintain. **Library and special JS feature usage** The `Object.keys()` method, which is used in the "without immutable" option, is a built-in JavaScript library that returns an array of a given object's own enumerable property names. This method does not require any external libraries or special JavaScript features. **Other considerations** * The benchmark assumes that both `baseObj` and `newObj` are objects. If either of them can be other types (e.g., strings, numbers), the results may vary. * The benchmark runs on a desktop machine with Chrome 67 and Mac OS X 10.13.5. **Alternative approaches** Other alternatives to these three options include: 1. **Lodash's `assignIn()` method**: This is another popular library for performing object assignments, which can provide more flexibility and control over the assignment process. 2. **Immutable data structures**: Using immutable data structures (e.g., Immutable.js) instead of mutable objects can eliminate the need for manual iteration or explicit immutability checks. 3. **Native Web Workers**: Running benchmarks in native Web Workers environments may help isolate performance variations due to different JavaScript engines and caching behaviors on different platforms. When choosing an object assignment method, consider factors such as code readability, maintainability, performance requirements, and potential edge cases that might affect the chosen approach.
Related benchmarks:
JavaScript spread operator vs Object.assign vs for-in loop performance
JavaScript spread operator vs Object.assign vs for-in loop safe performance
JavaScript spread operator vs Object.assign vs type-checked for-in loop performance
JavaScript spread operator vs Object.assign vs null-checked for-in loop performance
JavaScript spread operator vs Object.assign vs only-null-checked for-in loop performance
Comments
Confirm delete:
Do you really want to delete benchmark?