for (i = 0; i < 10; i++) {
}
const firstObject = {
sampleData: 'Hello world',
sampleMethod: () => { return true; }
};
const secondObject = {firstObject};
const firstObject = {
sampleData: 'Hello world',
sampleMethod: () => { return true; }
};
const secondObject = Object.create(firstObject);
const firstObject = {
sampleData: 'Hello world',
sampleMethod: () => { return true; }
};
const secondObject = Object.assign({}, firstObject);
class firstObject {
constructor() {
this.sampleData = 'Hello world',
this.sampleMethod = () => { return true; }
}
};
const secondObject = new firstObject();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Test Spread operator | |
Test Object.create | |
Test Object.assign | |
Test New Object |
Test name | Executions per second |
---|---|
Test Spread operator | 51637868.0 Ops/sec |
Test Object.create | 2057473.9 Ops/sec |
Test Object.assign | 19372564.0 Ops/sec |
Test New Object | 696882.6 Ops/sec |
This benchmark comparison focuses on various methods for creating and copying JavaScript objects, specifically testing the following techniques:
...
) new
)const secondObject = {...firstObject}; // using spread operator
const secondObject = Object.assign({}, firstObject);
const secondObject = Object.create(firstObject);
const secondObject = new firstObject();
cloneDeep
: A library offering deep cloning functionality, better suited for complex objects that include nested structures.Based on performance results, the spread operator demonstrates the best efficiency for copying and creating objects in simple use cases, while methods like Object.create
and using new
serve different purposes. While each technique has pros and cons, developers should choose based on the specific needs for object creation, inheritance, or property copying in their applications.