Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cost of adding new properties to object after creation.
(version: 0)
Comparing performance of:
object literal vs adding properties
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function objLiteral() { const obj = { _chanPutMsg_m: undefined, _doneVal_m: 3213, _name: true, _args: false, _state: "RUNNING", _doneCh: undefined, _childS: undefined, _sleepTimeout_m: undefined, _onCancel_m: undefined, _cancelCh: undefined, } return obj } function addProps() { const obj2 = {} obj2._chanPutMsg_m = undefined obj2._doneVal_m = 3213 obj2._name = true obj2._args = false obj2._state = "RUNNING" obj2._doneCh = undefined obj2._childS = undefined obj2._sleepTimeout_m = undefined obj2._onCancel_m = undefined obj2._cancelCh = undefined return obj2 }
Tests:
object literal
objLiteral()
adding properties
addProps()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object literal
adding properties
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object literal
730089856.0 Ops/sec
adding properties
21080048.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The provided JSON represents two JavaScript microbenchmarks: 1. **`objLiteral()`**: This function creates an object literal using the `const obj = { ... }` syntax. The objective of this benchmark is to measure the cost of creating a new object using an object literal. 2. **`addProps()`**: This function creates a new object by assigning properties to it, similar to how you would create an object literal. However, instead of directly creating the object, `addProps()` first creates an empty object (`const obj2 = {}`) and then assigns the properties. **Options Compared** Two approaches are being compared: 1. **Object Literal (`objLiteral()`)**: This approach creates a new object using the `const obj = { ... }` syntax. 2. **Property Assignment (`addProps()`)**: This approach first creates an empty object (`const obj2 = {}`) and then assigns properties to it. **Pros and Cons of Each Approach** 1. **Object Literal (`objLiteral()`)** * Pros: + More concise and readable syntax. + Can be faster because the object is created in a single statement. * Cons: + May incur additional overhead due to the need for type checking and syntax parsing. 2. **Property Assignment (`addProps()`)** * Pros: + Can be more efficient because it avoids the need for type checking and syntax parsing. * Cons: + Requires an extra step of creating an empty object before assigning properties. **Library Usage** None of the provided benchmarks use a library that affects the outcome of the benchmark. **Special JavaScript Features or Syntax** There is no special JavaScript feature or syntax being used in these benchmarks. The focus is on comparing two simple approaches to create objects. **Other Alternatives** If you wanted to add more complexity to this benchmark, here are some alternative approaches: * Using a class constructor instead of object literals. * Creating an object using the `Object.create()` method with a prototype. * Using a library like Lodash's `_.assign()` function to assign properties. Keep in mind that these alternatives may introduce additional variables and complexities that could affect the benchmark results.
Related benchmarks:
Thingie
Create object
Spread vs Object.assign (modify ) vs Object.assign (new)
javascript new vs Object.create 2
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?