Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object creation 3
(version: 0)
Comparing performance of:
Object spread vs Object.assign vs Babelified spread vs nothing
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var obj1 = {v1: 1, v2:2, v3:3, v4:4, v5:5, v6:5, v7:7, v8:8, v9:9, v10:10}
Tests:
Object spread
var obj2 = {...obj1}
Object.assign
var obj2 = Object.assign({}, obj1)
Babelified spread
var obj2 = _objectSpread({}, obj1);
nothing
obj1.v1; obj1.v2; obj1.v3; obj1.v4; obj1.v5; obj1.v6; obj1.v7; obj1.v8; obj1.v9; obj1.v10; obj1.v1; obj1.v2; obj1.v3; obj1.v4; obj1.v5; obj1.v6; obj1.v7; obj1.v8; obj1.v9; obj1.v10; obj1.v1; obj1.v2; obj1.v3; obj1.v4; obj1.v5; obj1.v6; obj1.v7; obj1.v8; obj1.v9; obj1.v10;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object spread
Object.assign
Babelified spread
nothing
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance can be a complex task, and this benchmark from MeasureThat.net provides a great example of how to compare different approaches. **What is being tested?** The benchmark tests the speed of three different methods for creating a shallow copy of an object: 1. **Object spread**: Using the syntax `{...obj1}` to create a new object with the same properties as `obj1`. 2. **`Object.assign()`**: Using the `Object.assign()` method to create a new object and then assigning the properties of `obj1` to it. 3. **Babelified spread**: Using the `_objectSpread()` function (which is a polyfill for the ES6 spread syntax) to create a new object with the same properties as `obj1`. **Options compared** The benchmark compares these three approaches: * Object spread: `{...obj1}` * `Object.assign()`: `var obj2 = Object.assign({}, obj1)` * Babelified spread: `_objectSpread({}, obj1)` (note that this is a custom implementation, not the standard ES6 spread syntax) **Pros and cons of each approach** 1. **Object spread**: This method is concise and easy to read, but it may not be supported in older browsers or environments. 2. **`Object.assign()`**: This method is widely supported, but it can lead to slower performance compared to other methods because it creates a new object and then assigns properties to it. 3. **Babelified spread**: This method is similar to the object spread syntax, but it uses a custom implementation instead of the standard ES6 syntax. In general: * Object spread is a good choice when you want a concise and easy-to-read solution, and you're sure your target environment supports it. * `Object.assign()` is a safe choice when you need to support older browsers or environments. * Babelified spread is a good alternative when you want a more efficient solution than object spread. **Library: Immutable** The benchmark includes the Immutable library (`https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.min.js`), which provides a way to work with immutable data structures in JavaScript. In this case, it's used as a dependency for the object spread and Babelified spread tests. **Special JS feature or syntax: ES6 spread syntax** The benchmark uses the ES6 spread syntax (`{...obj1}`) to create an object copy. This syntax is widely supported in modern browsers and environments, but it may not be supported in older browsers or environments. I hope this explanation helps!
Related benchmarks:
Object creation
Object assign vs polyfill
JavaScript spread vs Object.assign performance vs native spread
Object creation 2
Comments
Confirm delete:
Do you really want to delete benchmark?