Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object creation 2
(version: 0)
Comparing performance of:
Object spread vs Object.assign vs Babelified spread vs nothing
Created:
4 years ago
by:
Guest
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; 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; 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; 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):
Let's dive into the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided JSON represents a benchmark test case named "Object creation 2" from MeasureThat.net. It consists of four individual test cases: 1. Object spread 2. Object.assign 3. Babelified spread (a syntax that is compatible with modern JavaScript engines, including ES6+ features) 4. A "nothing" test case (no actual test) **What's being tested** In this benchmark, the test creator wants to compare the performance of different methods for creating a new object by copying an existing object (`obj1`). The test cases are: * Object spread (using the `_objectSpread` function) * `Object.assign` * Babelified spread (using the `_objectSpread` function with some syntax modifications) * A "nothing" test case that simply assigns values to variables, without creating a new object **Options comparison** Here's a brief overview of each option: 1. **Object spread**: * Pros: Simple and readable syntax; can be used with ES6+ features (e.g., rest parameter, destructuring). * Cons: May not work in older JavaScript engines or environments without `_objectSpread` support. 2. **Object.assign**: * Pros: Widely supported across different JavaScript engines and browsers. * Cons: Less readable syntax; may lead to errors if not used correctly (e.g., assigning properties instead of creating a new object). 3. **Babelified spread**: * Pros: Compatible with modern JavaScript engines, including ES6+ features; more readable than `Object.assign`. * Cons: May add unnecessary complexity due to the need for syntax modifications. **Benchmark results** The latest benchmark results show that: 1. Object spread has the highest number of executions per second (11430899). 2. `Object.assign` is second, with a lower execution rate (2649662.5). 3. Babelified spread has an intermediate execution rate (409400.4375), which is slower than object spread. 4. The "nothing" test case has the lowest execution rate (169329.625). **Conclusion** The benchmark results suggest that object spread is the fastest method for creating a new object by copying an existing one, followed closely by `Object.assign`. Babelified spread has an intermediate performance, and the "nothing" test case is the slowest. When to use each option: * Object spread: Use when working with ES6+ features or modern JavaScript engines. * `Object.assign`: Use in legacy environments or situations where compatibility is crucial. * Babelified spread: Use when you need a more readable syntax that still works across different browsers and engines.
Related benchmarks:
Object creation
Object assign vs polyfill
JavaScript spread vs Object.assign performance vs native spread
Object creation 3
Comments
Confirm delete:
Do you really want to delete benchmark?