Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance new objects
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign({}, firstObject, secondObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
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. **Benchmark Overview** The benchmark compares the performance of two approaches to merge two JavaScript objects: 1. Using the spread operator (`...`) 2. Using `Object.assign()` with an empty object (`{}`) as the target object. **What is being compared?** The test cases create two sample objects: `firstObject` and `secondObject`, each containing some data. The benchmark then measures the time it takes to merge these two objects using: 1. The spread operator (`...`) syntax, which creates a new object with all properties from both `firstObject` and `secondObject`. 2. `Object.assign()`, which merges the properties of both objects into a single target object. **Options Compared** The benchmark is testing the performance difference between these two approaches: Pros and Cons of each approach: * **Spread Operator (`...`)**: + Pros: concise, expressive syntax, creates a new object with all properties. + Cons: can be slower due to creation of a new object, potentially slower than `Object.assign()` for large objects. * **`Object.assign()`**: + Pros: generally faster for large objects, as it modifies the target object in-place. + Cons: less expressive syntax, can be confusing if not used correctly. **Library Usage** There is no specific library mentioned in the benchmark definition or test cases. However, `Object.assign()` is a built-in JavaScript method that is widely supported across browsers and platforms. **Special JS Features/Syntax** None are mentioned in this particular benchmark. The syntax used is standard JavaScript, with no special features or ES modules being utilized. **Other Alternatives** If you wanted to compare the performance of other object merging approaches, some alternatives could be: * Using `Object.create()`, which creates a new object and sets its prototype to the specified object. * Using `Object.defineProperties()` to merge properties from both objects into a single object. * Using libraries like Lodash (`_merge()`) or Immutable.js (`merge()`), which provide more comprehensive merging functionality. Keep in mind that these alternatives may have different performance characteristics compared to the spread operator and `Object.assign()`.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?