var params = { b:"hello", c: true, d:7, e: 'e', f: 'f', g: 'g', h: 'h', i: 1, j: 2, k: 3, l: 7, m: 7, n: 3, o: 'o', p: "azazazazazazazazaz", q: 'qqqqqqq' };
var other = Object.assign({ a: 2, aa: 2, aaa: 3, aaaa: 'aaaa' }, params);
var params = { b:"hello", c: true, d:7, e: 'e', f: 'f', g: 'g', h: 'h', i: 1, j: 2, k: 3, l: 7, m: 7, n: 3, o: 'o', p: "azazazazazazazazaz", q: 'qqqqqqq' };
var other = { a: 2, aa: 2, aaa: 3, aaaa: 'aaaa', params };
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
spread operator |
Test name | Executions per second |
---|---|
Object.assign | 1989243.0 Ops/sec |
spread operator | 2229028.0 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance difference between using Object.assign()
and the spread operator (...
) when assigning large objects to a new variable in JavaScript.
Benchmark Definition JSON
The benchmark definition json contains two test cases:
Each test case has a unique script preparation code that defines a large object params
with various properties, including strings, numbers, booleans, and other data types.
3. The first test case uses the Object.assign()
method to assign the params
object to another object other
.
4. The second test case uses the spread operator (...
) to create a new object by spreading the params
object's properties.
Options Compared
The benchmark compares two options:
...
): A newer, more concise way of creating objects by spreading existing objects' properties.Pros and Cons of Each Approach
Object.assign():
Pros:
Cons:
Spread Operator (...
):
Pros:
Object.assign()
Cons:
Library Used: None
There are no libraries used in this benchmark.
Special JS Feature/Syntax: None
This benchmark does not use any special JavaScript features or syntax, such as async/await, ES6 classes, or generators.
Other Alternatives
If you need to compare the performance of other assignment methods, consider these alternatives:
Keep in mind that these alternatives might not be as suitable for this specific benchmark, which focuses on object assignment.