Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance123d
(version: 0)
Comparing performance of:
Using the spread operator vs Using set
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const finalObject = { ...firstObject, data: 'foo bar', };
Using set
const firstObject = { sampleData: 'Hello world' } firstObject['data'] = 'foo bar'
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 set
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 break down what's being tested in this benchmark. **Benchmark Goal** The goal of this benchmark is to compare the performance of two approaches: using the JavaScript spread operator (`...`) and using the `set` method (specifically, assigning to an existing property). **Options Compared** Two options are being compared: 1. Using the JavaScript spread operator (`...`) 2. Using the `set` method (assigning to an existing property) **Pros and Cons of Each Approach:** ### Using the Spread Operator (`...`) * **Pros:** + More concise and readable code + Can be used to create new objects by copying existing ones, which can be more efficient than creating a new object from scratch * **Cons:** + May have performance overhead due to the creation of a new array or object property ### Using `set` Method * **Pros:** + Can be faster for simple assignment operations (assigning to an existing property) + Does not create a new property or array element, which can reduce memory allocation and deallocation * **Cons:** + Less concise and readable code + May require additional checks to ensure the property exists before assigning to it **Library Used** In this benchmark, no specific library is used beyond the JavaScript standard library. **Special JS Feature or Syntax** The `...` spread operator was introduced in ECMAScript 2015 (ES6) and allows for concise object creation. It's a relatively modern feature that has become widely supported across most browsers and engines. **Other Considerations** When deciding between these two approaches, consider the specific use case and performance requirements. If you need to create a new object by copying an existing one, using the spread operator might be more readable and efficient. However, if you're only assigning to an existing property, the `set` method might be faster due to its simplicity. **Alternatives** Other approaches for creating or modifying objects in JavaScript include: * Using object literals (`{ ... }`) * Using array destructuring (`[ ... ]`) * Creating a new object by calling `Object.assign()` with an empty object as the first argument Keep in mind that these alternatives may have different performance characteristics and readability trade-offs compared to using the spread operator or `set` method.
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?