Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Monomorphic vs Typed Monomorphic vs Empty Object prop assignment
(version: 2)
Comparing performance of:
Monomorphic vs Monomorphic Typed vs Empty Object
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const source = { a: "", b: "", c: "", setA: function() { this.a = ""; }, setB: function() { this.b = ""; }, setC: function() { this.c = ""; }, }; function assignProps1(target) { for (const key in source) { target[key] = source[key]; } }; function assignProps2(target) { for (const key in source) { target[key] = source[key]; } }; function assignProps3(target) { for (const key in source) { target[key] = source[key]; } }; const noop = function() {}; function getMonoData() { return { a: null, b: null, c: null, setA: null, setB: null, setC: null }; } function getTypedData() { return { a: "", b: "", c: "", setA: noop, setB: noop, setC: noop }; } function getEmptyData() { return {}; }
Tests:
Monomorphic
assignProps1(getMonoData());
Monomorphic Typed
assignProps2(getTypedData());
Empty Object
assignProps3(getEmptyData());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Monomorphic
Monomorphic Typed
Empty Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPad; CPU OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/132.0.6834.100 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 132 on iOS 18.1.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Monomorphic
35398024.0 Ops/sec
Monomorphic Typed
34668316.0 Ops/sec
Empty Object
19402314.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three different approaches for assigning properties to an object: 1. **Monomorphic**: Using a monomorphic function (`assignProps1`) that directly assigns values from an object (`source`) without any type checking or coercion. 2. **Typed Monomorphic**: Using a typed monomorphic function (`assignProps2`) that uses the `null` keyword to indicate that the assignment should be done using a specific data type (in this case, the data type of each property in the `source` object). 3. **Empty Object**: Using an empty object (`getEmptyData()`) as the target for property assignment. **Library Usage** In the benchmark code, two libraries are used: 1. `noop`: A no-op function that does nothing. Its purpose is to serve as a placeholder value for null values in the `source` object. 2. `assignProps1`, `assignProps2`, and `getEmptyData()` are custom functions defined in the benchmark script. **Special JS Features/Syntax** The benchmark uses some special JavaScript features: 1. **Object Spread Syntax**: The `const source = { ... }` syntax is used to define an object with multiple properties. 2. **Rest Parameters**: The `function assignProps3(target) {...}` syntax is used to define a function that takes a variable number of arguments (in this case, the keys of the `source` object). **Comparison of Approaches** Here's a brief summary of each approach and its pros and cons: 1. **Monomorphic (`assignProps1`)** * Pros: Simple, easy to read, and understand. * Cons: May lead to type coercion issues if the assigned value is not of the expected type. 2. **Typed Monomorphic (`assignProps2`)** * Pros: Provides explicit type checking and coercion for each assignment. * Cons: May be less efficient due to the overhead of null checks and type coercions. 3. **Empty Object (`getEmptyData()`)** **Other Considerations** The benchmark is designed to compare the performance of these three approaches on a variety of inputs (i.e., different values for `source`). This allows the benchmark to identify any potential performance differences between the three approaches. Overall, the benchmark provides valuable insights into the performance characteristics of monomorphic and typed monomorphic assignments, as well as the impact of using an empty object as a target.
Related benchmarks:
Object.assign vs mutation assign
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign mutation vs spread
Object.assign() vs Reflect.set()
Comments
Confirm delete:
Do you really want to delete benchmark?