<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
var params = { b:"hello", c: true, d:7 };
var other = Object.assign({}, params);
var params = { b:"hello", c: true, d:7 };
var other = JSON.parse(JSON.stringify({ b:"hello", c: true, d:7 }));
var params = { b:"hello", c: true, d:7 };
var other = $.extend(true, {}, params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
JSON | |
jQuery.extend |
Test name | Executions per second |
---|---|
Object.assign | 5497852.0 Ops/sec |
JSON | 1207102.4 Ops/sec |
jQuery.extend | 2736171.0 Ops/sec |
Let's break down the provided JSON and explain what is tested, the options compared, their pros and cons, and other considerations.
Benchmark Overview
The benchmark compares three JavaScript methods for creating shallow copies of objects: Object.assign
, JSON.parse(JSON.stringify())
, and $.extend(true, {})
(using jQuery). The goal is to determine which method performs best in terms of execution speed.
Options Compared
Pros and Cons
Library and Purpose
The JSON
library is used to parse JSON strings and create an object from it. The purpose of this method is to demonstrate the performance difference between serializing and deserializing objects.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in these benchmarks.
Other Considerations
Alternatives
JSON.parse(JSON.stringify())
. However, it is optimized for performance and provides more control over the cloning process.In conclusion, this benchmark provides a useful insight into the performance differences between three popular methods for creating shallow copies of objects in JavaScript. However, it's essential to consider the specific use case and potential edge cases before selecting an implementation.