const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const finalObject = ({obj2, obj })
const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const finalObject = Object.assign(obj2, obj);
const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
Object.assign({}, obj, obj2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Basic object spread | |
Basic assign operator | |
Assign with empty object |
Test name | Executions per second |
---|---|
Basic object spread | 765737.4 Ops/sec |
Basic assign operator | 4400208.0 Ops/sec |
Assign with empty object | 3729484.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark with three test cases, each testing a different approach to creating an object by spreading or assigning properties from two source objects.
Test Case 1: Basic Object Spread
The first test case measures the performance of creating an object using the spread operator ({...obj2, ...obj}
).
const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const finalObject = ({...obj2, ...obj});
Pros and Cons
Test Case 2: Basic Assign Operator
The second test case measures the performance of creating an object using the assignment operator (Object.assign(obj2, obj)
).
const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const finalObject = Object.assign(obj2, obj);
Pros and Cons
obj
and obj2
are objects) to ensure correct behavior.Test Case 3: Assign with Empty Object
The third test case measures the performance of creating an object using the assignment operator with an empty object (Object.assign({}, obj, obj2)
).
const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
Object.assign({}, obj, obj2);
Pros and Cons
Library Used: None
None of the test cases use any libraries, making them self-contained and easy to understand.
Special JS Features or Syntax: None
The benchmark does not use any special JavaScript features or syntax beyond what is standard in modern JavaScript.
Other Alternatives
Alternative approaches to creating objects could include:
Object.create()
methodcloneDeep()
function{...obj, ...obj2}
)However, these alternatives are not tested in this benchmark, and their performance may vary depending on specific use cases.
Benchmark Preparation Code: None
The provided JSON does not include any preparation code for setting up the test environment or loading dependencies.