<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
var params = { b:"hello", c: true, d:7 };
var other = Object.assign({ a: 2 }, params);
var params = { b:"hello", c: true, d:7 };
var other = { a: 2, params };
var params = { b:"hello", c: true, d:7 };
var other = $.extend({ a: 2 }, params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
spread operator | |
jQuery.extend |
Test name | Executions per second |
---|---|
Object.assign | 12940968.0 Ops/sec |
spread operator | 10954809.0 Ops/sec |
jQuery.extend | 6573381.0 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Benchmark Definition
The test compares three JavaScript methods for merging objects:
Object.assign()
...
)Options Compared
Each method has its own strengths and weaknesses, which we'll discuss next:
...
)Library
The test uses jQuery 3.1.1 as the reference implementation for jQuery.extend()
. The purpose of this function is to merge two or more objects into a single object, with the source objects' properties being overwritten by their destination values.
Special JavaScript Feature/Syntax
None are explicitly mentioned in this benchmark.
Other Alternatives
In addition to these three methods, other alternatives for merging objects include:
Object.create()
method with the assign
property.These alternatives may offer additional performance benefits or simplify code in specific use cases, but they are not part of the standard JavaScript API and may require additional setup.