Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
bechn Spread operator VS Object.assign VS Object.create
(version: 0)
Comparing performance of:
Test Spread operator vs Test Object.create vs Test Object.assign
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
for (i = 0; i < 10; i++) { }
Tests:
Test Spread operator
const firstObject = { sampleData: 'Hello world', sampleMethod: () => { return true; } }; const secondObject = {...firstObject}; Object.setPrototypeOf(secondObject, Object.getPrototypeOf(firstObject));
Test Object.create
const firstObject = { sampleData: 'Hello world', sampleMethod: () => { return true; } }; const secondObject = Object.create(Object.getPrototypeOf(firstObject), Object.getOwnPropertyDescriptors(firstObject));
Test Object.assign
const firstObject = { sampleData: 'Hello world', sampleMethod: () => { return true; } }; const secondObject = Object.assign({}, firstObject); Object.setPrototypeOf(secondObject, Object.getPrototypeOf(firstObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Test Spread operator
Test Object.create
Test 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):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare the performance of three different methods for creating shallow copies of objects in JavaScript: 1. **Spread Operator (`...`)**: Introduced in ECMAScript 2018, this operator allows you to create a new object by spreading the properties of an existing object. 2. **Object.assign()**: A built-in method that creates a shallow copy of an object. 3. **Object.create()**: A method that creates a new object with the specified prototype and inherits properties from it. **Pros and Cons of Each Approach** 1. **Spread Operator (`...`)**: * Pros: concise, readable, and efficient for simple objects. * Cons: may not work as expected when dealing with complex objects or inheritance, and can be slower than Object.assign() for large datasets. 2. **Object.assign()**: * Pros: widely supported, fast, and works well with most object types. * Cons: may require additional work to handle inherited properties correctly. 3. **Object.create()**: * Pros: allows for fine-grained control over inheritance and property creation. * Cons: can be less readable and more verbose than other methods. **Library Usage** None of the benchmark tests explicitly use any external libraries, but `Object.getOwnPropertyDescriptors()` is used in some test cases to create an object with inherited properties. This method is a part of the ECMAScript standard and does not require any additional library inclusion. **Special JS Feature/Syntax** The benchmark uses ES2018 syntax for the spread operator (`...`), which might not be supported by older JavaScript engines or environments that don't support this feature. **Other Alternatives** Besides the three methods being compared in this benchmark, other alternatives exist: * **lodash.cloneDeep()**: A popular utility library function that creates a deep copy of an object. * **immer.js**: A library that provides immutable data structures and functions for creating copies of objects. Keep in mind that these alternatives might have different performance characteristics or trade-offs depending on the specific use case. **Benchmark Results** The latest benchmark results show that `Object.assign()` is currently the fastest method, followed closely by the spread operator (`...`). The performance difference between these methods may become more pronounced as object sizes and complexity increase.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance create new
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?