Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object.assign vs new class extends
(version: 0)
Performance difference of creating a class that extends P and creating a new instance of it vs doing object.assign(new P(), ...)
Comparing performance of:
extend and create vs object.assign
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
extend and create
class Vehicle { constructor(name){ this.name = name; } } class Ev extends Vehicle { constructor(name, charge){ super(name); this.charge = charge; } } const car = new Ev("leaf",90);
object.assign
class Vehicle { constructor(name){ this.name = name; } } const car = Object.assign(new Vehicle("leaf"),{ charge: 90 })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
extend and create
object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
extend and create
585189.0 Ops/sec
object.assign
1227020.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents two individual test cases: 1. "object.assign vs new class extends": * This benchmark compares the performance of creating a class that extends another class (`new class extends`) with creating a new instance using `Object.assign()`. 2. The second set of test cases: * Each test case measures the performance of either creating an instance of a class by extending another class (`class Vehicle { ... }` and `class Ev extends Vehicle { ... }`) or using `Object.assign()` to create a new instance with initial values. **Options compared:** The benchmark compares two main approaches: 1. **Class inheritance**: Creating a new instance by inheriting from an existing class. 2. **Object assignment**: Using `Object.assign()` to create a new object with initial values. **Pros and Cons of each approach:** **Class Inheritance** Pros: * Encapsulates data and behavior within a single unit (the class). * Can provide additional benefits like polymorphism, inheritance, and composition. * Typically more readable and maintainable code. Cons: * May lead to over-engineering or excessive use of abstraction. * Can introduce complexity when trying to debug or optimize the class hierarchy. **Object Assignment** Pros: * Provides a lightweight and flexible way to create objects with initial values. * Can be faster than class inheritance, especially for simple cases. Cons: * Lacks encapsulation and data hiding. * May lead to tight coupling between classes if not used carefully. **Other considerations:** 1. **Library usage**: In the provided benchmark code, no specific libraries are mentioned. However, in general, JavaScript frameworks like React or Angular might influence the choice of class inheritance vs object assignment. 2. **Special JS features**: The benchmark does not explicitly use special JavaScript features like ES6 classes, async/await, or Promises. **Alternative approaches:** 1. **Prototypal inheritance**: Another way to achieve class-like behavior without using classes. 2. **Functional programming**: Using functions and function composition to create objects with initial values. 3. **Object creation libraries**: Specialized libraries like Lodash or Underscore.js provide optimized object creation methods. For the specific benchmark, it's essential to consider the trade-offs between encapsulation, readability, maintainability, and performance when choosing between class inheritance and object assignment.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign vs direct copy
Object.assign() vs Reflect.set()
Object.assign() vs spread operator (New object)
JavaScript: Normal assignation VS Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?