var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while(n.length < 1000) {
n = [n];
}
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while(n.length < 1000) {
n = JSON.parse(JSON.stringify(n))
}
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while(n.length < 1000) {
n = Object.assign({}, n);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread operator | |
JSON Parse | |
Object.assign |
Test name | Executions per second |
---|---|
Spread operator | 169160400.0 Ops/sec |
JSON Parse | 185737056.0 Ops/sec |
Object.assign | 179948880.0 Ops/sec |
Let's break down the benchmark definition and test cases.
Benchmark Definition: MeasureThat.net is testing three different approaches to create a new object reference:
Object.assign()
: This function creates a shallow copy of an existing object by merging all its enumerable properties into a new object.JSON.parse()
and JSON.stringify()
methods to convert an object to a JSON string, which can then be parsed back into an object.The test aims to measure the performance difference between these three approaches in creating a new object reference every time.
Options Compared:
JSON.stringify()
and then parses it back into an object using JSON.parse()
.Other Considerations:
Library Usage:
None of the benchmark definitions use external libraries or frameworks. However, it's worth noting that some browsers may have additional features or APIs that can affect the performance of these tests (e.g., Object.assign()
might be optimized in certain browsers).
Special JS Feature/Syntax: There is no special JavaScript feature or syntax used in these benchmark definitions.
Alternatives:
{...obj}
) instead of Object.assign()
.cloneDeep()
function.Keep in mind that these alternatives might not be as widely supported or optimized as Object.assign(), and their performance may vary depending on the specific use case and environment.