Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dynamic class vs object
(version: 0)
Comparing performance of:
dynamic class vs object vs static class
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function newObj() { return {aaa: 42, bbb: "hello"} } function newClass() { return new class { aaa = 42 bbb = "hello" } } class StaticClass { aaa = 42 bbb = "hello" } window.StaticClass = StaticClass
Tests:
dynamic class
let obj = newClass() window.aaa = obj.aaa window.bbb = obj.bbb
object
let obj = newObj() window.aaa = obj.aaa window.bbb = obj.bbb
static class
let obj = new StaticClass() window.aaa = obj.aaa window.bbb = obj.bbb
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
dynamic class
object
static class
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
dynamic class
527025.2 Ops/sec
object
3701650.2 Ops/sec
static class
3967411.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of three approaches: 1. **Dynamic Class**: A JavaScript class created using the `class` keyword, which will be instantiated as an object. 2. **Object**: An object created using a constructor function (`newObj`), which returns an object literal with properties `aaa` and `bbb`. 3. **Static Class**: A static class defined using the `class` keyword, but this time the class is not instantiated as an object. The benchmark prepares a JavaScript script that: * Defines two functions: `newClass()` and `newObj()`, which create instances of classes or objects, respectively. * Assigns properties to these instances on the global scope (`window.aaa` and `window.bbb`). * Measures the execution time for each test case. **Options Compared** The benchmark compares the performance of: * Instantiating a dynamic class using `new class{...}` vs creating an object using `newObj()`. * Creating a static class using `class{...}` vs assigning properties to an object created by `newObj()`. **Pros and Cons of Each Approach** 1. **Dynamic Class**: * Pros: Can be more expressive and easier to read, as it follows the traditional class syntax. * Cons: May have performance overhead due to the overhead of creating a new scope for each instance. 2. **Object**: * Pros: Faster execution time, as no scope creation is involved. * Cons: Less readable and may not be as expressive as using classes. 3. **Static Class**: * Pros: Can be faster execution time, as the class is not instantiated as an object. * Cons: May be less readable and less maintainable than traditional classes. **Library Used** None in this specific benchmark. **Special JS Feature or Syntax** This benchmark uses JavaScript's `class` keyword to define dynamic and static classes. This feature was introduced in ECMAScript 2015 (ES6) and provides a more concise way to define classes. **Other Considerations** * The benchmark is running on Chrome 120, which may affect the results due to various factors like just-in-time compilation, caching, etc. * The test cases are designed to be relatively simple and easy to understand, making it easier for users to compare and reproduce the results. **Alternatives** For those interested in exploring similar benchmarks, here are some alternatives: 1. **Benchmarking JavaScript classes**: A benchmark that compares the performance of different class creation methods (e.g., using `class` vs using a constructor function). 2. **JavaScript object creation benchmarks**: Benchmarks that compare the performance of creating objects using various methods (e.g., using literals, constructors, or classes).
Related benchmarks:
object.assign vs new class extends
class vs function constructor vs object literal vs __proto__ vs Object.create vs Object.setPrototypeOf
Comparison of classes vs prototypes vs object literals also including the inheritance
new object vs new instance of new class
Comments
Confirm delete:
Do you really want to delete benchmark?