Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread operator vs Object.assign vs Object.create V3
(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)); Object.keys(firstObject).forEach((key) => { if (RegExp('Method').test(key)) { Object.defineProperty(secondObject, key, { value: undefined }); } });
Test Object.create
const firstObject = { sampleData: 'Hello world', sampleMethod: () => { return true; } }; const secondObject = Object.create(Object.getPrototypeOf(firstObject), Object.getOwnPropertyDescriptors(firstObject)); Object.keys(firstObject).forEach((key) => { if (RegExp('Method').test(key)) { Object.defineProperty(secondObject, key, { value: undefined }); } });
Test Object.assign
const firstObject = { sampleData: 'Hello world', sampleMethod: () => { return true; } }; const secondObject = Object.assign({}, firstObject); Object.setPrototypeOf(secondObject, Object.getPrototypeOf(firstObject)); Object.keys(firstObject).forEach((key) => { if (RegExp('Method').test(key)) { Object.defineProperty(secondObject, key, { value: undefined }); } });
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):
**Overview** The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of three different approaches for creating objects in JavaScript: the spread operator (`...`), `Object.assign()`, and `Object.create()`. **What is being tested?** * The spread operator (`...`) creates a new object by spreading the properties of an existing object. * `Object.assign()` creates a new object by copying the specified properties from one or more source objects. * `Object.create()` creates a new object with the specified prototype, and optionally copies properties from another object. **Options compared** The benchmark test case compares the performance of these three approaches in creating objects. The specific test cases are designed to exercise different aspects of each approach. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Spread operator (`...`)**: + Pros: Simple, concise syntax; fast execution. + Cons: May not work as expected for certain types of objects (e.g., complex objects with nested arrays). * `Object.assign()`: + Pros: Works with any object type; supports optional source objects. + Cons: May be slower than the spread operator due to the overhead of copying properties. * `Object.create()`: + Pros: Provides a way to create objects with a specific prototype; useful for inheritance scenarios. + Cons: May be slower than the other two approaches due to the overhead of creating a new object and setting its prototype. **Library usage** None of the test cases use any external libraries. The `RegExp` class is used in the benchmark definitions to check if a key is a method. **Special JavaScript features or syntax** The spread operator (`...`) was introduced in ECMAScript 2015 (ES6). It's a concise way to create objects by spreading properties from another object. **Other alternatives** If you're interested in exploring alternative approaches, here are a few options: * `Object.assign()` variants: Some browsers have faster implementations of `Object.assign()`, such as `Object.assignSync()` or `Object.assign()` with an `options` object. * **Object.create()` alternatives: Other approaches to create objects with a specific prototype include using the `Function.prototype.constructor` property or creating a new function with the desired prototype. Keep in mind that these alternative approaches might not be as efficient or concise as the original spread operator, Object.assign(), and Object.create() methods.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance v2
JavaScript spread operator vs Object.assign performance create new
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?